Calling Win32 dlls

  • Thread starter Thread starter Otis Mukinfus
  • Start date Start date
O

Otis Mukinfus

// This is a declaration example for C:
int FAR PASCAL Search(int mode, LPSTR key1, LPSTR key2, LPSTR key3,
LPSTR found, int format);

// This declaration is for C#:
[DllImport("MyDll.DLL")]
private static extern int Search(int mode, string key1,
string key2, string key3, string dataFound, int format);


The above declarations declare a method that searches a file
containing string data.

When using the C# Declaration as below I cannot get a value back out
of the parameter dataFound. I have tried using both out and ref
parameter declarations for this parameter but both of them cause a
null reference exception.

I can Tell that the method is finding the data because the method is
returning an integer value greater that zero which indicates the
number of characters that were found.

Here is how I am calling the method:

string buffer = new string('\x0', 255);

int ret = MyDLL.Search(1, "BillyBob", "", "", buffer, 1);

What am I doing wrong?




Otis Mukinfus
http://www.otismukinfus.com
 
// This is a declaration example for C:
int FAR PASCAL Search(int mode, LPSTR key1, LPSTR key2, LPSTR key3,
LPSTR found, int format);

// This declaration is for C#:
[DllImport("MyDll.DLL")]
private static extern int Search(int mode, string key1,
string key2, string key3, string dataFound, int format);


The above declarations declare a method that searches a file
containing string data.

When using the C# Declaration as below I cannot get a value back out
of the parameter dataFound. I have tried using both out and ref
parameter declarations for this parameter but both of them cause a
null reference exception.

I can Tell that the method is finding the data because the method is
returning an integer value greater that zero which indicates the
number of characters that were found.

Here is how I am calling the method:

string buffer = new string('\x0', 255);

int ret = MyDLL.Search(1, "BillyBob", "", "", buffer, 1);

What am I doing wrong?




Otis Mukinfus
http://www.otismukinfus.com

I found the answer:

StringBuilder buffer = new StringBuilder( 1000 );
int ret = MyDLL.Search(1, "BillyBob", "", "", buffer, 1);
string result = buffer.ToString();


Otis Mukinfus
http://www.otismukinfus.com
 
Back
Top