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

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Interop Marshalling of C++ with pointers in the structure

Posted By:      Posted Date: September 05, 2010    Points: 0   Category :.NET Framework
I have a vendor supplied dll that is written in C++. I need to use it in a C# application and am trying to use Interop. I have .h files and example .cpp code from the SDK, but I'm clearly in over my head. There are dozens of functions defined in this API, but if I can just get started on how to do this, I'd appreciate some guidance. My biggest headeache is pointers. Here's examples of the C++ and how far I got with C# (which i'm sure I totally botched) MY.h: part of the included headers typedef struct my_handle *MY_Handle; MY_API_PUBLIC int _cdecl MY_HandleCreate(MY_Handle *my_handle, const char *serial_number, const char *client_id, const char *product_type, const char *product_version); MY_API_PUBLIC int _cdecl MY_HandleDestroy(MY_Handle *my_handle); MY_API_PUBLIC int _cdecl MY_Init(void); MY_API_PUBLIC int _cdecl MY_Shutdown(void); MY.cpp: some example functions. int MY_Init(void); int MY_HandleCreate(MY_Handle *my_handle, const char *serial_number, const char *client_id, const char *product_type, const char *product_version); int MY_HandleDestroy(MY_Handle *my_handle); int MY_Shutdown(void); MY.cs: The class created to house the interop stuff. class MYClass { //MY_Init() [DllImport("ts.dll", SetLastError = true)] public static extern int MY_Init(); //MY_Shutdown() [DllImport("ts.dll", SetLastError

View Complete Post

More Related Resource Links

How to pass double pointers for COM Interop via MethodInfo.Invoke ?

Hello, I need to make COM IntetrOp at runtime using reflections. My native COM Object's exposed methods have some parameters as pointers (DWORD*) and some double pointers (DWORD**) and some are user defined types(e.g SomeUDTType objSmeUDTType) and its pointer to pointer(i.e. SomeUDTType **pSomeUDTType). I need to pass pointers to pointers. For now how can I be able to populate "object" array as pointers to pointers of SomeUDTType. Working Example:      STDMETHODIMP MyCallableMethod(DWORD *value_1,BSTR *bstrName,WESContext **a_wesContext tlbImp.exe generated output:      DTINIDLLib.RuntimeCallingClass.MyCallableMethod(ref uint, ref string, System.IntrPtr) Regards UsmanMutlithreading

Problem : Need to make COM InterOp at runtime using reflections Passing Pointers as parameters?

Hello, I need to make COM IntetrOp at runtime using reflections. My native COM Object's exposed methods have some parameters as pointers (DWORD*) and some double pointers (DWORD**) and some are user defined types(e.g SomeUDTType objSmeUDTType) and vice versa its pointer(i.e. SomeUDTType *pSomeUDTType). Now for dynamic method invocation, we have single option for passing parameters as array of object i.e object[] and filling this array statically. But I need to pass pointers and references and pointers to pointers. For now how can I be able to populate object array as mixed data of simple data types, pointers or references and pointers to pointers. Working Example: Native COM exposed method : STDMETHODIMP MyCallableMethod(DWORD *value_1,BSTR *bstrName,WESContext a_wesContex Translated by tlbimp.exe (COMInterop) UDTINIDLLib.RuntimeCallingClass.MyCallableMethod(ref uint, ref string, UDTINIDLLib Now calling these methods at runtime using reflection at runtime, See here :        Assembly asembly = Assembly.LoadFrom("E:\\UDTInIDL\\Debug\\UDTINIDLLib.dll");         Type[] types = asembly.GetTypes();         Type type = null;    //foreach (Type oType in types)         {             try      &nb

Interop:Passing SOCKADDR structure as a parameter in C#

I have an external dll in c++ that I'm trying to Interop with C#. I have most of the function calls exposed, but I'm scrating my head on how to do this one. I can't figure out how to define and pass the sockaddr as a parameter. The unmanaged code has this .h: extern int MY_Connection(MY_Handle my_handle, struct sockaddr_storage my_ip); The example c++ code given to me is this: struct sockaddr_storage my_ip; ip_size = sizeof(my_ip); WSAStringToAddress("", AF_INET, NULL, (struct sockaddr *)&my_ip, &ip_size); int ret=MY_Connection(my_handle, my_ip); And I've only gotten this far in C#: [DllImportAttribute("my.dll", EntryPoint = "MY_Connection", CallingConvention = CallingConvention.Cdecl)] public static extern int MY_Connection(System.IntPtr my_handle, ???? And this is where I'm stuck. ???? ); IntPtr my_handle=new IntPtr(); int ret=MY_Connection(my_handle, ?????? ); How can I represent the socket structure? I know I'll have to do something like: [StructLayout(LayoutKind.Explicit, Size = ???)] public struct SOCKADDR_STORAGE { ???? } How would I pass the structure to the function? I'm a little confused.   Any help?  

Problem marshalling the following structure



I'm experiencing a problem in c# tryin to marshall a pointer to a structure inside another structure

I'm trying to call the following function hosted in a C++ library:

HRESULT GetNextRow(MYUSERDATA *userdata);

typedef struct _LDAPATTRIBUTE
  WCHAR attributeName[520];
  WCHAR attributeValue[520];

typedef struct _MYUSERDATA
  WCHAR objectGUID[80];
  WCHAR distinguishedName[520];
  PLDAPATTRIBUTE attributesList;

In C#, I have created the following definition:


    const int MAX_PATH = 260 * 2;
    const int MAX_OBJECTGUID = 40 * 2;

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct LDAPATTRIBUTE
      /// WCHAR[520]
      [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]

Create Interop dll from type library (Tlbimp) without marshalling attributes



I have the idl of com component, I generate type library using midl and use type library to generate interop dll. I want to create interop dll without any marshalling attribute, for example BSTR should be returned as IntPtr, not as string after marshalling. I could not find any attirbute in TlbImp to control the marshalling, how can I achieve this requirement?

COM interop: issue with C-style array of interface pointers


Dear community,

I have an issue with COM interop. I need to marshal a C-Style array of interface pointers.  I have changed IL to get this array marshaled correctly but I have issues accessing the array elements in the managed code.  Below are the IDL as well as my three tries that did not work.

Any suggestions?



[local] HRESULT Next([in] ULONG cElems, [out] ISomeObject *prgElems, [out] ULONG *pcFetched); \

COM Interop in C# 4.0

Let's take a bit of a recap of how far we've come. We've chatted about dynamic binding in C# and how that all plays in with the DLR, and about named and optional arguments and how they change the way methods are bound. The only other major piece in C# 4.0 is this notion of COM interop. We chatted about how dynamic really is a gateway to interop with different object models and languages (ie interacting with dynamic languages, dynamic object models, javascript objects, HTML DOM etc), but in C# 4.0, we want to go a bit further and provide you a few more tools to help make your interop life much easier.

Solution/project structure


Hello, I am working in a aspx web application that must be built in several separate "modules" with different functionality which will be partly developed by different teams.

However, I need the completed app to reside in ONE virtual directory. There will be a single startup page. I need session state to be preserved across pages in different modules.

I structured it as one solution with different Web Projects and one class library project with common functionalities.


1) is this the right approach to do this? Or which one would be better?

2) each time I add a new web project to the solution, a new web.config gets created. What do I do and where do I put them?

VS 2010 + framework 4.0 + TFS

Regarding TFS and source control, I have a single Team project, the solution and web projects are inside it.



Excel Interop Save as



I want to Save excel workbook using Excel interop in 97-2003 format. Currently I have Office 2007 installed on my machine.

below is the code I am using.

_book.SaveAs("New.xls", Excel.XlFileFormat.xlWorkbookDefault, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

The solution is little urgent.


.NET Interop: Automate Acceptance Testing With IronRuby


Acceptance testing verifies that the system under development meets the customer's requirements. Learn the benefits here.

Ben Hall

MSDN Magazine March 2009

.NET Interop: Getting Started With IronRuby And RSpec, Part 1


See how IronRuby lets you employ the straightforward Ruby language to create acceptance tests that interoperate with .NET-compliant code.

Ben Hall

MSDN Magazine February 2009

Visual Studio OBA Tools: Simplify OBA Development With Interop API Extensions


To introduce you to VSTO Power Tools Office interop API extensions, we'll walk through the development of an application that automates Outlook, Excel, and Word.

Andrew Whitechapel, Phillip Hoff, and Vladimir Morozov

MSDN Magazine December 2008

Interop: Connect To Mainframe Apps With BizTalk Adapters and .NET


If you have a significant investment in legacy COBOL applications running on mainframes, you need the BizTalk Adapters for Host Integration Server .

Bashar "Bash" Badawi

MSDN Magazine December 2007

CLR Inside Out: Introduction to COM Interop


The CLR allows seamless interactions between Microsoft .NET applications and COM. But how, exactly? The CLR team knows.

Thottam R. Sriram

MSDN Magazine January 2007

Interop: Get Seamless .NET Exception Logging From COM Clients Without Modifying Your Code


Using .NET objects from ASP can help you gain experience with the .NET Framework before migrating to ASP.NET. Your ASP pages will make use of the new .NET components through COM-callable wrappers (CCW). But how will you handle exceptions? This article fills you in.

Matt Adamson

MSDN Magazine January 2005

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