473,769 Members | 4,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(unsi gned int* handleArray,
unsigned* size);

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

[DllImport(DLL_N AME, CallingConventi on=CallingConve ntion.StdCall)]
public static extern UInt32
GetHandles([MarshalAs(Unman agedType.LPArra y)] 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 2066
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**********@h otmail.com> wrote in message
news:#j******** ******@tk2msftn gp13.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(unsi gned int* handleArray, unsigned* size);

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

[DllImport(DLL_N AME, CallingConventi on=CallingConve ntion.StdCall)]
public static extern UInt32
GetHandles([MarshalAs(Unman agedType.LPArra y)] 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
1852
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 means PE file and native dlls), if so how many ways to achieve it. Thank You
5
2956
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++ libraries. We are using managed C++ as a bridge between managed .NET code and unmanaged C++ code, which I'm sure is a fairly common practice. The managed C++ library is compiled with /CLR whereas all other libraries are compiled without /CLR because...
3
1863
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 framentation, but I don't see an alternative. Is there an easy way to generate a call to native malloc()? Doesn't this also fragment memory? (I'll be using C++/CLI, if that matters)
3
1433
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 assemblies to be executed must be tranlated to native code, the question is: Which is the time of live of the native assemblies? It is possible to configure the time? Are they storaged in some directory in my PC?
0
1518
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 Classlibrary (DxuCoreClr.dll) written in managed C++, that is encapsulating several native Api's in form of native Win32 dlls (i.e. dxldaputils.dll). The Classlibrary and the native dlls are installed to the bin directory of the Asp.Net 2.0...
3
3960
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 assemlies (DLLs) using MSVC8 (VS2005) with the /clr option. However, I get hundreds of linker errors (mostly LNK2028) because assembly B uses native types defined in assembly A. I could easily get rid of the problem by #including the original source...
3
1458
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 always thought that managed.dll must be copied to the application directory (where the .NET application runs) and that native.dll can be anywhere as long it is in PATH or is somehow else found by Windows. The problem is however that the .NET...
6
4597
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 have access via a protected "print"-method. I need the different dlls as the sub classes implement the base class's pure virtual methods using different technologies. To use the native classes from outside their dlls I use the...
6
6580
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 of native code which is wrapped by the managed C++/CLI code A remark to this complicated structure: We are controlling hardware via
0
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.