.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

C# and Unmanaged C++ dll

Posted By:      Posted Date: October 15, 2010    Points: 0   Category :ASP.Net

Hi all

I'm new to using InteropServices and have been presented with a C++ DLL that contains the coding for access to a scan device we are using.

I cannot for the life of me work out the correct way to call one of the functions.

Its definition is as follows

/*    HwDev_Execute(strDevInfoBuf,      //device information
                            nDevInfoLen,        //the length of strDevInfoBuf
                            strSendBuf,         //the command what is being seeded
                            nSendLen,           //the length of command(strSendBuf)

View Complete Post

More Related Resource Links

Use Unmanaged C++ class in .NET


Hi Everyone,

I have one project which is developed in unmanaged C++ code.

Now i want to use all unmanaged classes of C++ in .NET

I have one method called .NET wrapper in which i have to convert Unmanaged C++ class to managed C++ class which will then use as proxy class.

details for that are given in below link.


Is there any other solution for develop this?

CLR Inside Out: Marshaling between Managed and Unmanaged Code


Marshaling is an important part of .NET interop. It allows you to call into unmanaged code from managed code. This column will help you get started.

Yi Zhang and Xiaoying Guo

MSDN Magazine January 2008

.NET Internals: Examine Running Processes Using Both Managed and Unmanaged Code


There are plenty of times when you need to get information on running processes, not the least of which is during performance tuning. Using the techniques in this article and special .NET classes you'll see how to get a process' ID, name, priority, number of threads, kernel handle, and memory consumption, as well as its user-mode, kernel-mode, and total elapsed running time and put them to use in a custom app called AssemblyBrowser.

Christophe Nasarre

MSDN Magazine October 2004

Coroutines: Implementing Coroutines for .NET by Wrapping the Unmanaged Fiber API


Coroutines are a powerful feature of many programming languages including CLU, Scheme, Python, Ruby, and ICON. Coroutines can save processor overhead and reduce redundancy because they allow you to stop execution of a procedure midstream, return a value, and resume exactly where the procedure left off.This article shows how coroutines can be implemented for the .NET Framework by using the Fiber API and Managed Extensions for C++, and how they can be easily used with other .NET-compliant languages. This article also shows a sophisticated use of the runtime host for running multiple managed threads on a single OS thread.

Ajai Shankar

MSDN Magazine September 2003

C++ and ATL: Use ATL Server Classes to Expose Your Unmanaged C++ Code as an XML Web Service


Throughout this issue, you'll read all about the promise of Web Services and how the .NET Framework enables Web Service development. Many people will also be building their Web Services atop C++ code and frameworks like ATL Server, particularly when performance is paramount. In this article, the authors show how fully functional Web Services are built using ATL Server and Visual Studio .NET. Beginning with unmanaged C++ classes, they add ATL attributes that make the code work over HTTP.

Kirk Fertitta and Chris Sells

MSDN Magazine December 2002

SOAP: Using ATL Server to Build an Asynchronous SOAP Client in Unmanaged C++


SOAP opens up a new world of Web Services, letting you make function calls across a network or the Internet. But this flexibility creates new problems when your app needs to wait for calls to return from halfway around the world. What you need is an asynchronous SOAP client that takes advantage of threading to continue execution while waiting for calls over the wire. This article covers the basics of building such a client with ATL.

Pranish Kumar and Bogdan Crivat

MSDN Magazine April 2002

using managed code in unmanaged c++

I wanna use a managed c++ class and its functions (in which I wanna use interop services to wrap a COM based dll) in unmanaged codes is it possible? I mean, I feel like this maybe the case of JWNW(just will not work :D my alternate to IJW). This way seems to be scary and troubling  COM->C++/CLI->C++ and someone can remind me what was the function to call with System::Runtime::InteropServices (Visual c++ intellisense does not help me much there). Mmmm I had a few small projects calling unmanaged code in managed one ( using "pragma"s ) but I have no ideas for other way around. Can anyone direct me in right direction? I do not expect you to provide full working examples but seeing some conceptual FOO solutions would be most appreciated :D Thank you for your time :) Volkan

Sharing a file handler between managed and unmanaged code?

This is related to Interops, and sharing a file handler between a WPF UI and a native C++ DLL. I'm working on an application using MVVM as a guide. I want to use WPF for the UI, however it is a complex scientific application, which requires performance, and I am using C++ along with some GPU based code for all the modeling/processing. This is a Windows based app, no real network comm involved. I come across a challenge in terms of storing data. The data is multi-dimensional, and can be projected in many ways, while each projection can be processed/filtered in many ways too. I end up with a tree of data, with the raw data at the root and many branches. XML seems like an ideal way to store the information about this dataset, but as far as the raw data, it is not so convenient. I read about base64 encoding and the various binary-XML techniques, but see a major issue: the size of the converted data (150% of the raw) which in my case is huge (raw data is easily 200MBs+, and the multitude of projections can make it several 100's of MBs easily). There is the option of saving several files, but then it may become a nightmare for a user to 'transfer' files properly, so a one-file system seems best. I basically need to have the XML-like structure accessible to the UI, while the raw data needs to be accessed by the unmanaged code. So I was thinking I could concat

How to proceed with activeX and unmanaged win32 console[mix of a question and a discussion]

Hi I have rather weird situation. I want to get images from an ip camera to process with infamous opencv. My camera comes with description protocol for socket programmers however I do not wanna get distracted with developing that one as well. Anyway luckily there is also an MFC sample that comes with camera and it uses an activex control.My first question (1) Should I use the control as in given MFC window and then hide it from user somehow and find a way to marshal data to opencv code that I wrote or should I try to consume program in unmanaged C++(For latter one I worry whether juice worths the snooze)? Since I do not know so much about marshaling I am trying to solve this problem in 2nd method (trying to consume activex object as an ordinary com object). My second question comes here (2) I checked corresponding com object's clsid and tried to do this HRESULT hr; ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); CLSID clsid; const IID CLSID_AMC = { 0x745395C8, 0xD0E1, 0x4227, { 0x85, 0x86, 0x62, 0x4C, 0xA9, 0xA1, 0xA, 0x8D } }; clsid = CLSID_AMC; IDispatch *amc; hr = ::CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER /*this one was for activex right? */, IID_IDispatch, (void **)&amc); But it gave  0x80040154 Class not registered. Then changed last line to hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IDispatch, (void **)&amc

Unmanaged Code

What is the difference between UnManaged code & UnSafe code???? Explain with an example  please ???? 

Pointer into array when calling unmanaged code on 64 bit

I need to write a .Net-Frontend for an unmanaged Windows DLL which contains an API that must not be changed. My frontend works perfectly with the 32-bit version of the DLL. I have trouble using the 64-bit version of the DLL, because some functions expect a void pointer and interpret it as a pointer into an integer array. I boiled the problem down to this simple function: extern "C" int PASCAL EXPORT ExportedFunction(void *data) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // Hier normaler Funktionsrumpf int *arr = (int *)data; arr[0]++; arr[1]++; arr[2]++; return sizeof(int); } Here is a stripped down version of my attempts to call "ExportedFunction": Imports System.Runtime.InteropServices Public Class Form1 Declare Function ExportedFunction Lib "testlib.dll" (<MarshalAs(UnmanagedType.I4)> ByRef parameter As Integer) As Integer Declare Function ExportedFunction2 Lib "testlib.dll" Alias "ExportedFunction" (ByVal parameter() As Integer) As Integer Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim arr() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Dim intsize As Integer = ExportedFunction(arr(6)) Console.WriteLine("SizeOf int in DLL: " & intsize.ToString) Console.Wri

Problems with threading and unmanaged assembly

I am working on an application which has a main windows form with a timer (actually 2 timers but I want to get 1 working 1st).  The form has a form-level property which I store an instance of a class which calls functions in an unmanaged c++ assembly.  Initially, I had a button on the form which would create a new instance of the class and store it in the form-level property and make a call to the unmanaged code then start a timer.  The timer would then, at it's specified interval, also reference the form-level property and making it's own calls to the unmanaged code.  This caused issues including the lovely blue screen of death.  I assume this was because I had instanciated the class at the form level in the form's thread and then the timer in it's thread was also trying to use the form-level property which was instanciated in a thread that was not the timer's thread.  At this point I changed my timer tick method to create a new instance of the necessary class and store that to the form-level property.  This still seems to be causing issues as well. Not knowing that much about threading I'm a little lost on how best to organize my code.  Any guidance?

Using unmanaged DLL (installed in WinSxS) in VSTO Excel addin.

Hello, I have developed an Excel VSTO addin, written in C#. From the addin I make calls into unmanaged assembly through pinvoke. My unmanaged assembly is properly installed in Windows Side By Side cache (WinSxS). I can call its methods from other programs if I add the assembly as a dependency to an application manifest. Unfortunately I cannot make my addin find my unmanaged assembly. Every call throws a File Not Found exception. The only way that works is adding the unmanaged DLL as a dependency in excel.exe.manifest. I know this method is dirty and I don't want to use it. Even if I simply put my unmanaged DLL into my addin's directory, the addin can then find the DLL but not its dependent MFC libraries, so the calls still fail. I have tried adding MFC manifest as a dependency to my addin project but id didn't work. My question is: is there any way to make VSTO addin "see" WinSxS unmanaged DLL? Thank you, Jimmy Joe

InterOp: Use an unmanaged DLL in a .NET project (Nikon SDK).

Hi all!I want to wrap a DLL so that it can be used in a .NET project.More specific: The Nikon SDK (MAID3) provides a couple of header-files (*.h), one DLL and one MD3-file (which I think is an DLL also).The DLL contains all base functionality and the MD3 file contains functionality specific for one type of camera.The functionality found in the DLL is for controlling a camera through code.The problem is that I'm not sure how to import the DLL.I've tried adding it as a reference to the .NET project. No luck though, since it's not a COM-DLL.The same goes for .NET Reflector which couldn't load it.I loaded the DLL using an InterOp native call to LoadLibrary and then a call to GetProcAddress. Both methods worked fine, the DLL was loaded and I got a reference to the EntryPoint-method through GetProcAddress. The name of the EntryPoint-method was found using dumpbin.exe /exports DLLNAME.dll. The thing I noticed was that the only method exported was the entrypoint method. None of the methods described in the documentation / header files were outputted.I guess that if I had a reference to the other methods / structures I could make it work.I also tried using DLLImport("PATHTODLL", EntryPoint:="ENTRYPOINTMETHODNAME") but I couldn't verify that it pointed to the right method.Where should I go from here?Kind regards/ Richard

Unmanaged dll called from webservice

Hi there,I have a webservice that wraps an unmanaged C++ dll.  I'm wondering how I should handle state and multiple connections?  I'm thinking that the unmanaged dll will be shared across all connections...is this the case?  Note that I am using a shared host so I have limited abilities to modify IIS specifically.  If the unmanaged dll is not shared and will have a unique instance for every connection then I have nothing to worry about.  If it's the case it's shared then I guess I will have to persist the unmanaged state on a per-connection basis through a DB?Thanks!Craig Clayton

debug unmanaged dll


Hi all,

I have one Windows application. I have to access a function in unmanaged code(c++ dll) from my managed code(c#).

I have done that using dllimport. Now i have to debug that unmanged c++ code. I have all ".cpp" file.

How to debug the code ????????

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend