473,395 Members | 1,666 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,395 software developers and data experts.

DllImport into C#

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
6 5896
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jim | last post by:
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the windows console the...
9
by: Ole Christensen | last post by:
I'm trying to make a sort of conditional compilation in my C# code because my app is intended to run on both a Pocket PC and on a normal desktop PC. My code uses a call to an API function that on...
3
by: Mark Jerde | last post by:
I'm sill learning VS .NET 2003, not an expert yet. I'm calling an unmanaged C++ DLL from C# using . When the whole project is done I will be calling a total of 5 C++ DLLs from C#. All the DLLs...
1
by: Brian Anderson | last post by:
Hello, I have a native, C++ console app that uses ~26MB of RAM when it runs. It uses quite a lot of RAM to make some math but will never xceed 26MB. Now I've made a dll out of this code and...
2
by: Brian Anderson | last post by:
Hello, is it possible to use DllImport to call a DLL in ASP.NET ? Or is it necessarry that my DLL has to be copied into \System32 ? My DLL is a native C++ 7.1 DLL (not managed, no COM, no...
2
by: Ed | last post by:
Hello, dear all, I often see these two import usage in the code. Both are the interface to use the Dll library. I think they are the same. Normally P/Invoke means using the to import the dll....
1
by: kardon33 | last post by:
Let me explain my problem, Im currently trying to use a Perl module that was built for a Windows OS that uses a .dll and .lib file. I have obtained the c header files that the modules were built...
9
by: jjones7947 | last post by:
Am doing a JNI wrap on a C++ API, am using VC7 and Eclipse. In preparation, I created a C++ executable which mimicked the flow of the JNI, i.e. a driver file which called methods in file with methods...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.