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

Calls to native dlls from C#

dln
Hey all. I'm a bit new to the language and I'm trying to figure out how to
have my c# application interact with native code that is exported via a dll.
I've run into a problem interfacing with a native dll method where one or
more of the parameters in the native routine is an array or pointer to block
of contiguous memory. So, for example, the method as exported from the
native Dll is declared as:

extern "C" unsigned int __stdcall GetHandles(unsigned int* handleArray,
unsigned* size);

Where "handleArray" is an array of unsigned integers and "size" is the
number of elements in the array. My c# function import statement reads:

[DllImport(DLL_NAME, CallingConvention=CallingConvention.StdCall)]
public static extern UInt32
GetHandles([MarshalAs(UnmanagedType.LPArray)] ref UInt32[] handles, ref
UInt32 size);

When I call the native method from my c# code, I can step into the native
code and the method operates exactly as I would expect it to. However, when
returning from the native code, an exception is thrown which appears to be
related to the passing of the unsigned integer array. If I remove the "ref"
specification for the array, the code is executed with no errors. However,
I see the native code filling in the array but the array returned to my c#
code is empty (so it appears to me that the memory referenced by the c#
array and the memory passed to the native routine are two different blocks
of memory). I've also tried replacing the "ref" specifier with "out", but
the same exception is thrown when returning from the native code (does "out"
and "ref" equate to the same thing in c#?).

After going through the MSDN documentation on marshalling and calling native
routines from c#, I can't say that I fully understand how I can pass arrays
(or any data buffer for that matter) back and forth between native code and
c#. Can anybody tell me where I'm going wrong?

Thanks.
Nov 15 '05 #1
1 2025
dln
....I figured it out. I needed to remove the "ref" specification on the
array and add a SizeParamIndex=1 to the MarshalAs declaration. Thanks
anyway.

"dln" <dn**********@hotmail.com> wrote in message
news:#j**************@tk2msftngp13.phx.gbl...
Hey all. I'm a bit new to the language and I'm trying to figure out how to have my c# application interact with native code that is exported via a dll. I've run into a problem interfacing with a native dll method where one or
more of the parameters in the native routine is an array or pointer to block of contiguous memory. So, for example, the method as exported from the
native Dll is declared as:

extern "C" unsigned int __stdcall GetHandles(unsigned int* handleArray, unsigned* size);

Where "handleArray" is an array of unsigned integers and "size" is the
number of elements in the array. My c# function import statement reads:

[DllImport(DLL_NAME, CallingConvention=CallingConvention.StdCall)]
public static extern UInt32
GetHandles([MarshalAs(UnmanagedType.LPArray)] ref UInt32[] handles, ref
UInt32 size);

When I call the native method from my c# code, I can step into the native
code and the method operates exactly as I would expect it to. However, when returning from the native code, an exception is thrown which appears to be
related to the passing of the unsigned integer array. If I remove the "ref" specification for the array, the code is executed with no errors. However, I see the native code filling in the array but the array returned to my c#
code is empty (so it appears to me that the memory referenced by the c#
array and the memory passed to the native routine are two different blocks
of memory). I've also tried replacing the "ref" specifier with "out", but
the same exception is thrown when returning from the native code (does "out" and "ref" equate to the same thing in c#?).

After going through the MSDN documentation on marshalling and calling native routines from c#, I can't say that I fully understand how I can pass arrays (or any data buffer for that matter) back and forth between native code and c#. Can anybody tell me where I'm going wrong?

Thanks.

Nov 15 '05 #2

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

Similar topics

3
by: rashid | last post by:
Sorry for posting this message on general . kindly direct this message to the specific group. I know we can invoke the Native Code from .Net but can we generate Native Code in .Net( Native Code...
5
by: Adam McKee | last post by:
We are using Visual Studio.NET 2003 in our project with .NET framework 1.1. One of our libraries is a mixed-mode dll assembly consisting of one managed C++ library, and several unmanaged C++...
3
by: -DG- | last post by:
I'm still trying to figure out some of the nuances of access to legacy Win32 DLLs. I need to alloc buffers to be used by the Win32 DLLs. I know that pinning a managed pointer can lead to...
3
by: Xavi Sam | last post by:
Hi When I build my asp.net application the ASP.NET generates a net.assembly by page in the directory of my pc: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files Theese...
0
by: Reini | last post by:
We are developing an Asp.Net 2.0 application (Web Administration) for the IIS 5.0 to 6.0 and the W2K to W2K3 operating system. The application consists of several layers. One layer is a .Net 2.0...
3
by: dfranzen | last post by:
Hi *, I'm in the process of porting an ANSI C++ project from a UNIX platform to .NET. After "getting rid of" some platform-specific stuff I managed to compile the first two libraries into mixed...
3
by: Boris | last post by:
We have a native Windows DLL (let's call it native.dll) we developed a .NET interface for (let's say managed.dll). When a .NET application now uses the ..NET interface to talk to the Windows DLL we...
6
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I have a class hierarchy distributed over 3 native C++ dlls. The base class has a .NET Windows.Form for status output via a gcroot<>. The gcroot is declared private - the sub classes only...
6
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hi, I have a pretty complicated program which consists of the following basic modules: 1. a C# application 2. a C++/CLI application which is controlled by the C# app via IPC remoting 3. lots...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.