By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,456 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,456 IT Pros & Developers. It's quick & easy.

DllImport into C#

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <Ti***********@nospamaddress.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #2

P: n/a
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" <Ti***********@nospamaddress.com> wrote in message news:<uB**************@tk2msftngp13.phx.gbl>...
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" <Ti***********@nospamaddress.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #3

P: n/a
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" <te**@intex.com> wrote in message
news:86**************************@posting.google.c om...
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" <Ti***********@nospamaddress.com> wrote in message

news:<uB**************@tk2msftngp13.phx.gbl>...
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" <Ti***********@nospamaddress.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #4

P: n/a
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" <Ti***********@nospamaddress.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <te**@intex.com> wrote in message
news:86**************************@posting.google.c om...
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" <Ti***********@nospamaddress.com> wrote in message

news:<uB**************@tk2msftngp13.phx.gbl>...
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" <Ti***********@nospamaddress.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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
>
>


Nov 15 '05 #5

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #6

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.