win32 DLL and LPTSTR

S

Steven Blair

Hi there.

Having a problem calling a win32 function from a dll.
Here is the function header:

DWORD thrdGetHcNum (DWORD nPort, DWORD nBaudrate, LPTSTR lpszHcNum)

//Description of the 3rd parameter
LpszHcNum: The head of the buffer area used to store the serial number
of the terminal, 9 bytes at least.

Here is my code:

[DllImport("comdll.dll", SetLastError = true)]
public static extern uint thrdGetHcNum(int nPort, int nBaudrate,
[MarshalAs(UnmanagedType.LPTStr)] ref StringBuilder lpszHcNum);

//The actual call
StringBuilder Buffer = new StringBuilder(10);
result=thrdGetHcNum(Port,Baud,ref Buffer);

Everytime I runt his, I get a Object not set to reference error.
Can anyone tell me what I am doing wrong?
I have tried using a string, a char[] buf = new[10], MarshalAs.LPTstr
and MarshalAs.Array but always hit the same problem.

I am calling other functions from the dll without any problem. The only
difference with this one is I should be getting a string back with some
data in it.

Any help on this would be appreciated.

Regards,

Steven




*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
W

Wiktor Zychla

[DllImport("comdll.dll", SetLastError = true)]
public static extern uint thrdGetHcNum(int nPort, int nBaudrate,
[MarshalAs(UnmanagedType.LPTStr)] ref StringBuilder lpszHcNum);

I think that "ref" is not necessary here. Reference types are always passed
by reference. With "ref" you pass a reference to the reference.

[DllImport("comdll.dll", SetLastError = true)]
public static extern uint thrdGetHcNum(int nPort, int nBaudrate,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpszHcNum);

I hope this will help
Regards, Wiktor Zychla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top