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

Top 5 Contributors of the Month
Post New Web Links

Marshal a C# struct to native C++

Posted By:      Posted Date: April 10, 2011    Points: 0   Category :WPF

Platform is WS 2008 RC2, VS 2010.

I have what I think should be a pretty straightforward marshalling situation, but it just isn't working as expected.

Native Code:

#pragma pack(push, 8)
struct Handle
unsigned long Type;
void* Value;
void* Owner;
#pragma pack(pop)

__declspec(dllexport) Handle __cdecl CreateStyleProvider(const wchar_t* provider);
__declspec(dllexport) Handle __cdecl CreateOutputGroup(long ordinal, Handle style);

The Handle struct is really just opaque data that gets passed around.

C# code:

[StructLayout(LayoutKind.Sequential, Pack=8)]

View Complete Post

More Related Resource Links

Marshal C++ Struct to C# class?

Is it possible to have the Marshaller move a C++ struct to a C# class? I defined the C# class with StructLayout Explicit, and mapped it. It works perfectly as a struct, but if I change to class, I get a buffer overflow. Is there some way to work around this? I need to add methods to these "classes", and making a wrapper class that has all the same fields seems like such a waste.

COM Interop, Marshal C# IEnumerator->Native C++ IEnumVARIANT?


Hi folks,

I have a C# Windows Service which hosts a COM class whose client is C++ native code. On one method of the interface, there is the potential for a lot of data to be returned. Internally, the method is using an ArrayList of structs. The method has an [out] parameter typed as IEnumerator. In the method, I assign ArrayList.GetEnumerator() to this out parameter. The Type library export is getting it right; it is typing the out parameter as IEnumVARIANT, as expected.

However, on the client side, when I try to call Next() on the IEnumVARIANT interface pointer, I get an E_INVALIDARG HRESULT. Since the arguments to Next() are of the correct types, I'm at a loss as to what the problem is, and am thinking I'm doing something wrong on the Host side. Can anyone help me out here?

Many thanks!

Regards, D. T. Doutel

Marshal a number of strings in struct


Hi, I confuse with marshaling strings in struct. I want output many strings in C++ Dll once a time to C# code. But it always has some problems.

I tried to use this way:


 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct DcmRecord
      public String PatientsBirthDate;
      public String PatientsSex;
      public String PatientsName;
      public String PatientID;
      public String PatientsBirthTime;
      public String OtherPatientIDs;
      public String OtherPatientNames;
      public String EthnicGroup;
      public String NumberofPatientRelatedStudies;
      public String NumberofPatientRelatedSeries;
      public String NumberofPatientRelatedInstances;

    [return: MarshalAs(UnmanagedType.I1)]

CLR Inside Out: Best Practices For Managed And Native Code Interoperability


There are many factors to consider when building your app with both managed and native code. Find out how to employ interop and how to choose the interop that's right for you.

Jesse Kaplan

MSDN Magazine January 2009

GUI Library: Bring the Simplicity of Windows Forms to Native Apps


In this article, author John Torjo presents a guide to his C++ GUI library called eGUI++ and explains how it makes user interface programming easier.

John Torjo

MSDN Magazine June 2008

WinUnit: Simplified Unit Testing for Native C++ Applications


Maria Blees introduces WinUnit, a handy new unit testing tool for native C++ projects.

Maria Blees

MSDN Magazine February 2008

XmlLite: A Small And Fast XML Parser For Native C++


XmlLite provides a high-performance, low-overhead XML reader and writer geared for applications written in native C++. Learn more here.

Kenny Kerr

MSDN Magazine April 2007

Netting C++: Mapping Native C++ to the Common Type System


This month Stanley Lippman begins translating the Text Query Language Query class hierarchy from C++ to the .NET Common Type System.

Stanley B. Lippman

MSDN Magazine February 2007

Wrappers: Use Our ManWrap Library to Get the Best of .NET in Native C++ Code


By now you're probably used to reaching into the .NET Framework using Managed Extensions with your C++ code. But what do you do if you have legacy apps that use older versions of the compiler, or if you want to avoid some of the CLR requirements? Well, you can wrap Framework classes in a native way so you can use them in any C++/MFC app without /clr. Paul DiLascia shows you how.

Paul DiLascia

MSDN Magazine April 2005

XML in Yukon: New Version Showcases Native XML Type and Advanced Data Handling


The next version of Microsoft SQL Server, code-named "Yukon," represents quite a few steps forward in the evolution of XML integration. Yukon supports native storage of XML data using the XML data type, which makes it possible to run native queries on XML data using the emerging industry standard XQuery language. Data integrity of the XML data type can be enforced through schema validation and XML-based check constraints, and special indexes can be defined that help speed up queries. In addition, Yukon has the built-in ability to expose its data through Web services. This article discusses these and other XML features of Yukon.

Bob Beauchemin

MSDN Magazine February 2004

Custom Struct with maxvalue

I am trying to think whether the follwoing is possible? Like when we assign say 23423423423432423 to <short> datatype error is thrown in VS itself. Can we define our own datatype with say 0-100 range, and it throws error by marking the value underlined as blue and error message in the vs itself. thanks kunal

'mscorlib.dll' targets a different processor/.net calling a native dll

I  have a vb.net that calls a dll in native cpp.  Works fine in 32bit.  Now I want to upgrade to 64 bit.  I've done this before and it worked but now get a runtime error.  Compiles ok but with the error noted in the subject line. Also, when I try to debug, the ide tells me that it can't debug managed and native.  Yes it can, I just forgot how. platform is win7, intel 64 bit.  any suggestion to the forum/whitepaper or ideas are greatly appreciated. Thanks in advance! Robert.

AppDomain.Unload effects Unable to evaluate expression because the code is optimized or a native fra

Hello, I create a custom AppDomain.Create().  I registered the Unhandled Exception Event from my custom AppDomain. In the Unhandled Exception Event i try to Unload my AppDomain with AppDomain.Unload. But this effects a "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." Have anyone a idea what i can do against this behaviour. greetints

Can I marshal the same object to two URIs?

Due to external constraints, I have to provide remoting access to the same Singleton object through two URI's. Omitting all the remoting configuration (which does seem to set up correctly), the general idea is: m_RemotedComponent = new RemotedComponent(); RemotingServices.Marshal(m_RemotedComponent, remotingName1); RemotingServices.Marshal(m_RemotedComponent, remotingName2); The two connections provide different interfaces that the one component supports and are accessed by different clients. Regardless of the order in which the two Marshal statements appear, the first one works and the second one doesn't. Upon attempting to connect to the second URI (regardless of whether I have an active connection to the first URI) using Activator.GetObject(...), I get the error message saying that the object has to derive from MarshalByRefObject. But, since I can always instantiate the first-listed URI successfully, it obviously does derive from that. If I create a second RemotedComponent, I can marshal it successfully to the second URI, so the problem is clearly related to trying to marshal the same object to two URI's. Is this supposed to be possible? If so, how?

Install SQL Server2005 Error:[Native Client]Encryption not supported on the client

Product: Microsoft SQL Server 2005 -- Error 29515. SQL Server Setup could not connect to the database service for server configuration. The error was: [Microsoft][SQL Native Client]Encryption not supported on the client. Refer to server error logs and setup logs for more information. For details on how to view setup logs, see "How to View Setup Log Files" in SQL Server Books Online.

Error: A native exception has occurred in my_file.exe

Hello everyone,   This error is occurring with me when I am working with SSCE in my pocket PC. Can anyone tell me why?   Thanks!
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