C# DLL marshal struct* : MarshalAs question

M

Mo

Hi,

i am trying to call unmanaged C++ DLL in my C# application
i converted all the structures to be .net compatible (e.g TCHAR to
string (including Marshal As)
one of the functions i try to call is defined in the DLL
bool hhpConnect(Connection_tag* ptag, PTCHAR msg);

where Connection_tag is defined as
typedef Connection_tag
{
.......
}

when i try to call it in C#, i use [DllImport("MyDLL.dll",
CharSet=CharSet.Auto)]

i am not sure how to call the Pointer to a structure? should i use
public static extern bool hhpConnect([Ref,
MarshalAs(UnmanagedType.Struct)]
ref Connection_tag ptag,[MarshalAs(UnmanagedType.ByValTStr)] msg);

i am confused, the *ptag would hold the connection setup parameters..?
also, i think i need to MRashal the string msg too?
how this be different if *ptag was to be changed in the hhpConnect?

Thanks ALL

Mo
(e-mail address removed)
 
M

Mattias Sjögren

i am not sure how to call the Pointer to a structure? should i use
public static extern bool hhpConnect([Ref,
MarshalAs(UnmanagedType.Struct)]
ref Connection_tag ptag


You're right that you should use ref Connection_tag as the parameter
type. But there's no RefAttribute, and you shouldn't use
UnmanagedType.Struct here (you don't need any MarshalAs attribute).

,[MarshalAs(UnmanagedType.ByValTStr)] msg);

Leave out the MarshalAs attribute here too, and add the parameter type
string.

i am confused, the *ptag would hold the connection setup parameters..?

Can't say without knowing more about the API you're calling.

how this be different if *ptag was to be changed in the hhpConnect?

Changed in what way?



Mattias
 
M

Mo

Mattias Sjögren said:
i am not sure how to call the Pointer to a structure? should i use
public static extern bool hhpConnect([Ref,
MarshalAs(UnmanagedType.Struct)]
ref Connection_tag ptag


You're right that you should use ref Connection_tag as the parameter
type. But there's no RefAttribute, and you shouldn't use
UnmanagedType.Struct here (you don't need any MarshalAs attribute).

,[MarshalAs(UnmanagedType.ByValTStr)] msg);

Leave out the MarshalAs attribute here too, and add the parameter type
string.

i am confused, the *ptag would hold the connection setup parameters..?

Can't say without knowing more about the API you're calling.

how this be different if *ptag was to be changed in the hhpConnect?

Changed in what way?



Mattias

Hi Mattias,
thanks for the help.
I assume if the *ptag structure has to be changed in that function ,
passing it as a ref should be fine (thats what i meant)!!
this leads me to another question, when do i have to use
hhpconnect ([In (or Out),MarshalAs(UnmanagedType.Struct)] ref
Connection_tag ptag) ....? cause i have seen some examples like this
with little info about it.

Take care
Mo
 

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