Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem Converting DLL Structure to corresponding C# Types

2 views
Skip to first unread message

Jitu

unread,
Dec 17, 2003, 12:31:31 PM12/17/03
to
I'm trying to use a C dll provided by our vendor which
has structure definition

typedefstruct tag FCSDKSTRUCT{

WORD funcno;
char foldername[11];
WORD totalindex;
char indextitle[12][31];
char indexvalue[12][31];
WORD autonext;
char errormsg[80];


} FCSDKSTRUCT;

//prototype

int FCSDKAPI(FCSDKSTRUCT* pfcsdkstruct);

into a C# program.

Here is my code

namespace DocMgrP01
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///



public class FrmDocSearch :
System.Windows.Forms.Form
{
[StructLayout(LayoutKind.Sequential)]
// public struct FCSDKSTRUCT
public class FCSDKSTRUCT
{
public short FuncNo;

[MarshalAs
(UnmanagedType.ByValTStr, SizeConst = 11)]
public string FolderName;

public short TotalIndex;

// [MarshalAs
(UnmanagedType.LPArray , SizeParamIndex = 12, SizeConst =
31)]
[MarshalAs
(UnmanagedType.LPArray , SizeConst = 372)]
public char[,] IndexTitle;


// [MarshalAs
(UnmanagedType.LPArray , SizeParamIndex = 12, SizeConst =
31)]
[MarshalAs
(UnmanagedType.LPArray , SizeConst = 372)]
public char[,] IndexValue;

public short AutoNext;

[MarshalAs
(UnmanagedType.ByValTStr, SizeConst = 80)]
public string ErrorMessage;
}

[DllImport(@"C:\Program
Files\Resolutions\RFileMgr\Client\Program\fcsdkdll.dll",
CharSet = CharSet.Unicode)]
// [DllImport("fcsdkdll.dll", CharSet =
CharSet.Auto)]
public static extern int FCSDKAPI
(FCSDKSTRUCT pfcsdkstruct);

private System.Windows.Forms.TextBox
textSSN;
private System.Windows.Forms.TextBox
textLName;
private System.Windows.Forms.TextBox
textFName;
private System.Windows.Forms.TextBox
textMName;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button
btnSearch;
private System.Windows.Forms.Button
btnCancel;
private System.Windows.Forms.Label label5;
private
System.Windows.Forms.CheckedListBox chklstDocType;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container
components = null;

public FrmDocSearch()
{
//
// Required for Windows Form
Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code
after InitializeComponent call
//
FCSDKSTRUCT ss = new FCSDKSTRUCT
();

ss.IndexTitle = new char[12,31];
ss.IndexValue = new char[12,31];

ss.FuncNo = 1;

ss.FolderName = "HRTEST";

// ss.IndexTitle[0,0] = "SSN";
// ss.IndexTitle[0,1] = "LAST NAME";
// ss.IndexTitle[0,2] = "FIRST NAME";
// ss.IndexTitle[0,3] = "MIDDLE
NAME";
// ss.IndexTitle[0,4] = "DOCTYPE";

ss.IndexTitle[0,0] = 'S';
ss.IndexTitle[0,1] = 'S';
ss.IndexTitle[0,2] = 'N';


// for(int i=5;i<12;i++)
// {
// ss.IndexTitle[i] = "";
// }

// ss.IndexValue[00] = "439-43-0675";
// ss.IndexValue[01] = "WILLIAMS";
// ss.IndexValue[02] = "NATASHA";
// ss.IndexValue[03] = "D";
// ss.IndexValue[04] = "I-9";

ss.IndexValue[0,0] = '4';
ss.IndexValue[0,1] = '9';
ss.IndexValue[0,2] = '3';

// for(int i=5;i<12;i++)
// {
// ss.IndexValue[i] = "";
// }

// ss.AutoNext = 1;
ss.ErrorMessage = "";

int sdkrt = FCSDKAPI(ss);
if (sdkrt != 0)
{
MessageBox.Show
(ss.ErrorMessage.ToString(),"Error",

MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}


Thank you.

Mattias Sjögren

unread,
Dec 19, 2003, 6:50:48 PM12/19/03
to

>int FCSDKAPI(FCSDKSTRUCT* pfcsdkstruct);
[...]

> [DllImport(@"C:\Program
>Files\Resolutions\RFileMgr\Client\Program\fcsdkdll.dll",
>CharSet = CharSet.Unicode)]
>// [DllImport("fcsdkdll.dll", CharSet =
>CharSet.Auto)]
> public static extern int FCSDKAPI
>(FCSDKSTRUCT pfcsdkstruct);


The struct parameter should have the ref modifier applied to it.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

anon...@discussions.microsoft.com

unread,
Dec 22, 2003, 8:39:01 AM12/22/03
to

>-----Original Message-----
>
>>int FCSDKAPI(FCSDKSTRUCT* pfcsdkstruct);
>[...]
>> [DllImport(@"C:\Program
>>Files\Resolutions\RFileMgr\Client\Program\fcsdkdll.dll",

>>CharSet = CharSet.Unicode)]
>>// [DllImport("fcsdkdll.dll", CharSet =
>>CharSet.Auto)]
>> public static extern int FCSDKAPI
>>(FCSDKSTRUCT pfcsdkstruct);
>
>
>The struct parameter should have the ref modifier
applied to it.
>
How to define and call it in the program

>
>
>Mattias
>
>--
>Mattias Sjögren [MVP] mattias @ mvps.org
>http://www.msjogren.net/dotnet/ |
http://www.dotnetinterop.com
>Please reply only to the newsgroup.
>.
>

Mattias Sjögren

unread,
Dec 22, 2003, 3:06:02 PM12/22/03
to

>How to define and call it in the program

[DllImport("fcsdkdll.dll")]
public static extern int FCSDKAPI(ref FCSDKSTRUCT pfcsdkstruct);

...

int sdkrt = FCSDKAPI(ref ss);

anon...@discussions.microsoft.com

unread,
Dec 23, 2003, 11:07:57 AM12/23/03
to
Thanks alot.

Another Question how do i Marshall

char indextitle[12][31];
char indexvalue[12][31];

>-----Original Message-----
>
>>How to define and call it in the program
>
>[DllImport("fcsdkdll.dll")]
>public static extern int FCSDKAPI(ref FCSDKSTRUCT
pfcsdkstruct);
>

>....


>
>int sdkrt = FCSDKAPI(ref ss);
>
>
>
>Mattias
>
>--
>Mattias Sjögren [MVP] mattias @ mvps.org
>http://www.msjogren.net/dotnet/ |
http://www.dotnetinterop.com
>Please reply only to the newsgroup.

>.
>

Mattias Sjögren

unread,
Jan 1, 2004, 11:47:18 AM1/1/04
to

>Another Question how do i Marshall
>
>char indextitle[12][31];
>char indexvalue[12][31];

What you had was almost correct, just change LPArray yo ByValArray

[MarshalAs(UnmanagedType.ByValArray , SizeConst = 372)]
public char[] IndexValue;

0 new messages