DllImport into C#

  • Thread starter Thread starter Tim Mulholland
  • Start date Start date
T

Tim Mulholland

Whats the correct C# datatype (or marshalling function or something) to use
when you're importing a function that has a signature similar to

char* FuncA(char c[])

?

Assuming you know that both the parameter for the function and the return
value are c-style strings?

Thanks in advance!

Tim
 
just so you know, what i'm trying is

[DllImport("MyDll.dll", EntryPoint="FuncA")]
private static extern IntPtr FuncA([MarshalAs(UnmanagedType.LPStr)] string
ParamA);

as per something i saw a little bit below in the Interop NG.. but i'm
getting a NullReferenceException when i make the calls to that function and
i'm wondering if its because of how i'm declaring it.
 
I was able to get something similar to work by using StringBuilder:

... FuncA( StringBuilder string ParamA ) ;

It seems that the DllImport's default char set is ANSI so StringBuilder
marshals the parameter correctly. Also, my C function was declared
as CDECL and I had to specify the calling convention in the DllImport statement.

Ted


Tim Mulholland said:
just so you know, what i'm trying is

[DllImport("MyDll.dll", EntryPoint="FuncA")]
private static extern IntPtr FuncA([MarshalAs(UnmanagedType.LPStr)] string
ParamA);

as per something i saw a little bit below in the Interop NG.. but i'm
getting a NullReferenceException when i make the calls to that function and
i'm wondering if its because of how i'm declaring it.

Tim Mulholland said:
Whats the correct C# datatype (or marshalling function or something) to use
when you're importing a function that has a signature similar to

char* FuncA(char c[])

?

Assuming you know that both the parameter for the function and the return
value are c-style strings?

Thanks in advance!

Tim
 
It was your post i was referring to, so thanks for responding.
What do you mena you had to specify the calling convention in the DllImport
statement?

I'm still very stumped about why i get an exception when i don't debug the
unmanaged code, but no exception when i do...

Ted Sung said:
I was able to get something similar to work by using StringBuilder:

.. FuncA( StringBuilder string ParamA ) ;

It seems that the DllImport's default char set is ANSI so StringBuilder
marshals the parameter correctly. Also, my C function was declared
as CDECL and I had to specify the calling convention in the DllImport statement.

Ted


"Tim Mulholland" <[email protected]> wrote in message
just so you know, what i'm trying is

[DllImport("MyDll.dll", EntryPoint="FuncA")]
private static extern IntPtr FuncA([MarshalAs(UnmanagedType.LPStr)] string
ParamA);

as per something i saw a little bit below in the Interop NG.. but i'm
getting a NullReferenceException when i make the calls to that function and
i'm wondering if its because of how i'm declaring it.

Tim Mulholland said:
Whats the correct C# datatype (or marshalling function or something)
to
use
when you're importing a function that has a signature similar to

char* FuncA(char c[])

?

Assuming you know that both the parameter for the function and the return
value are c-style strings?

Thanks in advance!

Tim
 
n/m, i found what the CallingConvention thing was. i declare mine with

__declspec(dllexport)

which one should i be using? They all seem to do the same thing (throw that
exception)

Tim Mulholland said:
It was your post i was referring to, so thanks for responding.
What do you mena you had to specify the calling convention in the DllImport
statement?

I'm still very stumped about why i get an exception when i don't debug the
unmanaged code, but no exception when i do...

Ted Sung said:
I was able to get something similar to work by using StringBuilder:

.. FuncA( StringBuilder string ParamA ) ;

It seems that the DllImport's default char set is ANSI so StringBuilder
marshals the parameter correctly. Also, my C function was declared
as CDECL and I had to specify the calling convention in the DllImport statement.

Ted


"Tim Mulholland" <[email protected]> wrote in message
just so you know, what i'm trying is

[DllImport("MyDll.dll", EntryPoint="FuncA")]
private static extern IntPtr FuncA([MarshalAs(UnmanagedType.LPStr)] string
ParamA);

as per something i saw a little bit below in the Interop NG.. but i'm
getting a NullReferenceException when i make the calls to that
function
and
i'm wondering if its because of how i'm declaring it.

Whats the correct C# datatype (or marshalling function or something) to
use
when you're importing a function that has a signature similar to

char* FuncA(char c[])

?

Assuming you know that both the parameter for the function and the return
value are c-style strings?

Thanks in advance!

Tim
 
Tim,
n/m, i found what the CallingConvention thing was. i declare mine with

__declspec(dllexport)

which one should i be using?

__declspec(dllexport) indicates that the function is to be exported,
it doesn't specify the calling convention. You use __stdcall, __cdecl
etc for that. The default is probably cdecl unless you cahnged it with
some compiler option.



Mattias
 
Back
Top