473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

P Invoke Question

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

Apr 26 '07 #1
8 1684
"ba.hons" <ba*****@gmail.comwrote in message
news:11*********************@t39g2000prd.googlegro ups.com...
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.
Apr 26 '07 #2
Hi,

"ba.hons" <ba*****@gmail.comwrote in message
news:11*********************@t39g2000prd.googlegro ups.com...
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?
Apr 26 '07 #3
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
Apr 26 '07 #4
"Serge Baltic" <ba*****@hypersw.netwrote in message
news:dc**************************@news.microsoft.c om...
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.

Apr 26 '07 #5
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
Apr 26 '07 #6
"Serge Baltic" <ba*****@hypersw.netwrote in message
news:dc**************************@news.microsoft.c om...
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

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.
Apr 26 '07 #7
On Apr 26, 6:49 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Serge Baltic" <balt...@hypersw.netwrote in message

news:dc**************************@news.microsoft.c om...
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

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

Apr 27 '07 #8
"ba.hons" <ba*****@gmail.comwrote in message
news:11**********************@t38g2000prd.googlegr oups.com...
On Apr 26, 6:49 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
>"Serge Baltic" <balt...@hypersw.netwrote in message

news:dc**************************@news.microsoft. com...
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

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


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

Willy.
Apr 27 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JPRoot | last post by:
Hi I wish to set/get a property using System.ComponentModel.ISynchronizeInvoke.Invoke but I cannot find the exact syntax.. Any clue how to do this (right now I am making GetEnabled/SetEnabled...
3
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
1
by: RickDee | last post by:
Hi. I need help badly here. I need to write a program to invoke methods ( static, public, nonstatic, with parameters, and etc ) from an exe file, but I keep facing problem. From all the notes...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i donīt want to filter the return value of the method, i have to pass a new instance of the...
15
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
5
by: J.Evans.1970 | last post by:
Hi. I've inherited a Web Service from a developer who is no longer here. One of the purposes of the service was to clear the cache on the server side. Up until a few days ago, it worked fine -...
2
by: archana | last post by:
Hi all, I am having application wherein i am using asynchronous programming and using callback i am updating one label control. My question is can i use lock statment to lock control. If yes...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
2
by: Vighneswar | last post by:
Hi All I am doing with a standalone application in .NET, where I have to invoke the Outlook / Outlook Express by passing account info (username, password, pop3 server, ... etc) and SQL Server...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.