P Invoke Question

B

ba.hons

Hello,

I have started to use an old WIN32 API to develop an application and
am having trouble executing one of the DLL's methods.

int ContactController(char *ipHostName, int port)

I have my code as follows:

[DllImport("MYDLL.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern int
ContactController([MarshalAs(UnmanagedType.LPStr)]String test,
int Port);

I have also tried

[DllImport("MYDLL.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern int ContactController(String test,
int Port);

Unfortunately i dont receive an exception only an Integer which is
returned from the method.

I presume i am not marshalling the char* correctly.

Anyone with any help would be appreciated!

Thanks

Adam
 
W

Willy Denoyette [MVP]

ba.hons said:
Hello,

I have started to use an old WIN32 API to develop an application and
am having trouble executing one of the DLL's methods.

int ContactController(char *ipHostName, int port)

I have my code as follows:

[DllImport("MYDLL.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern int
ContactController([MarshalAs(UnmanagedType.LPStr)]String test,
int Port);

I have also tried

[DllImport("MYDLL.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern int ContactController(String test,
int Port);

Unfortunately i dont receive an exception only an Integer which is
returned from the method.

I presume i am not marshalling the char* correctly.

Anyone with any help would be appreciated!

Thanks

Adam

You should never expect an exception to be returned from an unmanaged
function. What's the value returned and what's the documented meaning of
this return value?


Willy.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

ba.hons said:
Hello,

I have started to use an old WIN32 API to develop an application and
am having trouble executing one of the DLL's methods.

int ContactController(char *ipHostName, int port)

I have my code as follows:

[DllImport("MYDLL.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern int
ContactController([MarshalAs(UnmanagedType.LPStr)]String test,
int Port);

I have also tried

[DllImport("MYDLL.dll", SetLastError = true, CharSet =
CharSet.Auto)]
static extern int ContactController(String test,
int Port);

Unfortunately i dont receive an exception only an Integer which is
returned from the method.

It's impossible to receive an exception from a unmanaged method, Exceptions
you see are managed classes :)

From the signature it seems that this method is meant to open a connection
to a remote host, the int returned should be the handler of the connection.

Do you have docs for the API?

Could you rewrite the API in .net?
 
S

Serge Baltic

Hello,
int ContactController(char *ipHostName, int port)
[DllImport("MYDLL.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int
ContactController([MarshalAs(UnmanagedType.LPStr)]String test,
int Port);

The function wants CharSet.Ansi, while you define an interop for it with
CharSet.Unicode, which effectively delivers a zero-sized string to the callee.

(H) Serg
 
W

Willy Denoyette [MVP]

Serge Baltic said:
Hello,
int ContactController(char *ipHostName, int port)
[DllImport("MYDLL.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int
ContactController([MarshalAs(UnmanagedType.LPStr)]String test,
int Port);

The function wants CharSet.Ansi, while you define an interop for it with
CharSet.Unicode, which effectively delivers a zero-sized string to the
callee.

(H) Serge

Where did you see this CharSet.Unicode?
There is a marshaling attribute "UnmanagedType.LPStr", telling interop to
marshal 'test' as ANSI string.

Willy.
 
S

Serge Baltic

Hello,
Where did you see this CharSet.Unicode?

CharSet.Auto on Windows NT.
There is a marshaling attribute "UnmanagedType.LPStr", telling interop
to marshal 'test' as ANSI string.

Overlooked this particular thing, sorry.

(H) Serge
 
W

Willy Denoyette [MVP]

Serge Baltic said:
Hello,


CharSet.Auto on Windows NT.




Overlooked this particular thing, sorry.

(H) Serge

Yep, but the marshaling attribute overrules this one and serves only for
function name mapping. The CharSet attribute in this case isn't even
required IMO.

Willy.
 
B

ba.hons

Yep, but the marshaling attribute overrules this one and serves only for
function name mapping. The CharSet attribute in this case isn't even
required IMO.

Willy.



I eventually did change the charset attribute to ANSI and this solved
the issue!

thanks for all the help guys!

Adam
 
W

Willy Denoyette [MVP]

ba.hons said:
I eventually did change the charset attribute to ANSI and this solved
the issue!

thanks for all the help guys!

Adam



As I said, it's not required, ANSI is the default, but I also prefer to be
explicit.

Willy.
 

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