marshalling

Best Way To Marshal A Pointer of Array of Struct

I'm calling functions from C++ that returns a pointer to an array of struct and I'm having problems since I'm new to this operation/implementation. My C++ codes: // My C++ Structs typedef struct _MainData { double dCount; DataS1 *DS1; int iCount1; DataS2 *DS2; int iCount2; }MainData; typedef struct _DataS1 { ...

Ways To Marshal A Pointer of Array of Struct

I'm calling functions from C++ that returns a pointer to an array of struct and I'm having problems since I'm new to this operation/implementation. My C++ codes: // My C++ Structs typedef struct _MainData { double dCount; DataS1 *DS1; int iCount1; DataS2 *DS2; int iCount2; }MainData...

Marshal.PtrToStructure throwing System.ArgumentException error.

I'm attempting to get a KBDLLHOOKSTRUCT from a keyboard-hook's lParam. private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { KBDLLHOOKSTRUCT kbd = new KBDLLHOOKSTRUCT(); Marshal.PtrToStructure(lParam, kbd); // Throws System.ArguementException ... Unfortunately the PtrToStructure...

marshalling custom directshow filter interface in c#

Hey folks, I've been stuck on this for the past few days. I have a custom COM interface that I am able to cast to, but when I call one if it's functions I get an AccessViolationException because the this pointer has changed. The below code represents the two interfaces that I am marshalling. All methods result in the same problem. Any...

Marshalling an object that has object fields

Not sure if the title makes any sense. I have an object that I want to marshal using JAXB that looks like this: @XmlRootElement(name = "subscriptionRequest") public class RegistrationRequest { private Long id; private RegistrationSource registrationSource; } The RegistrationSource object: public class RegistrationSo...

I successfully called advapi32's LsaEnumerateAccountRights() from C#. Now how do I unmarshal the array of LSA_UNICODE_STRING it returns?

It's a pointer to an array of LSA_UNICODE_STRING structures. I found some code that does the inverse, i.e., create a LSA_UNICODE_STRING from a C# string. You can see that in the helper code section below. What I have up to and including the call to LsaEnumerateAccountRights() seems to work just fine. Sensible values are returned for the...

How to marshal a C++ interface to C#, e.g. IObserver in GOF observer pattern

I have a C++ base code using GOF observer pattern, and I need a UI object, written in C#, act as an observer. I'd like to use C++ Interop as a wrapper, and let C# object inherit the IObserver which is written in C++, but it seems that C++ Interop cannot do it. Here's the code in C++, class IObserver { public: virtual void Update(vo...

Cannot call COM object created from STAThread from oher STA threads

I am new to COM and trying to understand the difference between STA and MTA. I tried to create an example that would show that COM can manage calls to object created in STA that is not thread-safe. MyCalcServer class here is created using ATL Simple Object. The settings used are the same as in this article: Threading Model: Apartment...

How to read a Delphi Array of Fixed Sized Strings within a packed record in c#

I need to read a blob field from a database into a c# app. However the blob field was written to the database by a Delphi App using the following method: procedure WriteABlob(Blob : TBlobField; var Buffer; size : integer); var s : String; begin setlength(s,size); move(buffer,s[1],Size); Blob.Value := S; end; ...

Convert array<Byte>^ data to const byte* data - C++/CLR

Hello I am trying to call a function in C from C# though c ++ so basically C# -> C++ - >C In C#, I have byte[] bytes - which reads the information from the file. I am passing the byte array and the size to C++ . In C++ I get the byte array and the size but I am not able to convert to the specific data types. void Image::OpenMemFile(...

How to marshal pointer types when calling C dll function in C#

I need to call a c function imported from a dll. I can find how to import the api function but I am unsure how to marshal all the types the function expects as paramters. Particularly pointer types. Example function signature might be... Result_Type get_signal_value( SIG* sig, char* name, int *value ); where SIG is a sturcture conta...

JAXB 2.0 - How to skip processing of nodes and return them as a String

Hello, I am processing XML documents with JAXB 2.0 where I need information(in my example 'id') mapped to Java objects and run some business logic with. Everything works fine here. But these XML documents always hold a collection of approximately 500 nodes that I just want to save to a database in the original xml format, so I am jus...

How to call unmanaged c++ function that allocates output buffer to return data in c#?

I have problems with marshalling output parameter of c++ function returning array of data to c#. Here is C++ declaration: #define DLL_API __declspec(dllexport) typedef TPARAMETER_DATA { char *parameter; int size; } PARAMETER_DATA; int DLL_API GetParameters(PARAMETER_DATA *outputData); The function allocates memo...

Marshal.PtrToStringUni() vs new String() ?

Suppose i have a pointer of type char* to unicode string, and i know the length: char* _unmanagedStr; int _unmanagedStrLength; and i have 2 ways to convert it to .NET string: Marshal.PtrToStringUni((IntPtr)_unmanagedStr, _unmanagedStrLength); and new string(_unmanagedStr, 0, _unmanagedStrLength); In my tests, both calls gives me...

Help me convert C++ structure into C#

Hello, I am completely new to C#, and need help converting a C++ structure to C#. The C++ structure is given as: #define QUE_ADDR_BUF_LENGTH 50 #define QUE_POST_BUF_LENGTH 11 typedef struct { const WCHAR *streetAddress; const WCHAR *city; const WCHAR *state; const WCHAR *country; const WCHAR *postalCode; } QueS...

Marshal safearray of struct inside struct

I have the following code in C++ which I need to be able to call from C#: struct Inner { double data1; double data2; }; struct Outer { double data3; SAFEARRAY innerData; }; int WINAPI ProcessData (Outer& outer ) { ... } I tried the following but it did not work What am I doing wrong? [StructLayoutAttribute(LayoutKind.Sequent...

Marshalling what is it and why do we need it?

What is marshalling and why do we need it. I find it hard to believe that i cannot send an int over the wire from c# to c and have to marshall it. Why cant c# just send the 32bits over with a starting and terminating signal, telling C code that it has received an int. If there are any good tutorials or sites about why we need marshallin...

How to pass a pointer to a struct via Pinvoke?

I am trying to write the C# equivalent to the following: typedef struct BATT_ID { UINT8 nBattID[8]; } BATT_ID, *PBATT_ID; HANDLE g_hDevice; // Connect to the driver g_hDevice = CreateFile(L"BAT1:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); void GetB...

Marshaling a C++ two-dimensional fixed length char array as a structure member

I am trying to call an unmanaged C++ function, that has a structure as an input parameter. The structure is defined in the header file like this: struct MyStruct { int siOrder; char aaszNames[6][25]; int siId[6]; int siTones[6]; }; I tried to declare the managed struct as following: [StructLayoutAttribute(Layo...

Passing a struct pointer as a parameter in C#

Hello all, I have a function in C++ that I exported to a DLL. I contains a struct pointer as one of the parameters. I need to use this function in C#, so I used DLLImport for the function and recreated the struct in C# using StructLayout. I've tried passing in the parameter using ref as well as tried Marshaling it in using MarshalAs(U...