6

It feels like my problem is similar to this.

So far, I have a struct defined in C++ like this:

typedef struct struct_type1
{
    uint64  nameLen;
    char *  name;  
} STATUSSTRUCT;

and a function defined as:

extern int _stdcall getStatus(STATUSSTRUCT * status);

and presumably the function like this:

int _stdcall getStatus(STATUSSTRUCT * status)
{
    status->nameLen = someLength;
    status->name = someName;
    return 1;
}

Note that I can't actually change the C++ code (for various reasons) nor the header file.

My C# code looks like this:

public struct STATUSSTRUCT
{
    public UInt64 nameLen;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4128)]
    public byte[] name;
}
STATUSSTRUCT status;

[DllImport("test.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int getStatus(ref STATUSSTRUCT status);

public void refreshStatus() {
    status = new STATUSSTRUCT();
    status.nameLen = 4128;
    status.name = new byte[4128];
    getStatus(ref status);
}

However, calling refreshStatus is giving me System.AccessViolationException.

Can someone help me figure out how I can call this function in C++ from C#?

3
  • Isn't C# a garbage-collected language? If so,what if that name is garbage collected by C#? What will the C struct name point to? What you probably would have to do (and this is bad enough) is for your C++ code to allocate memory and then copy the data into name, not just store the pointer. Aug 25, 2016 at 20:27
  • If your problem is about pointers, you can allow unsafe code in the project properties so you can use pointers with C# (define Char* in C#) Aug 25, 2016 at 20:30
  • @PaulMcKenzie: "Isn't C# a garbage-collected language?" - Yes. Just like C++. Neither language automatically destroys objects, while they are still in use, though. One significant difference however is, that .NET has a compacting heap, and objects can move around in memory during their life time. To avoid this (when passing the structure to unmanaged code) you could use the fixed keyword, or call GCHandle.Alloc with a handle type Pinned. Aug 25, 2016 at 20:49

2 Answers 2

3

Your structure expects a pointer to an array; you are marshalling the array. One side of the transaction is expecting to get the address "123 Sesame Street" and you are providing an exact replica of the apartment building at that address. That's not going to work.

To get marshalling code correct you need to have a thorough and deep understanding of memory management in C#. My advice is that you obtain the services of an expert.

1
  • 2
    @FrankJ: That's absolutely right. There is not enough information in the question to give an answer that is likely to be correct, and asking probing questions to determine that information is best done by an expert who has a thorough and deep understanding of memory management. If you are such an expert, then I encourage you to give an answer that you like better. Aug 30, 2016 at 21:19
2

You may try to use StringBuilder instead of byte[] and LPStr instead of ByValArray:

public struct STATUSSTRUCT
{
    public UInt64 nameLen;
    [MarshalAs(UnmanagedType.LPStr, SizeConst = 4128)]
    public StringBuilder name;
}

status = new STATUSSTRUCT();
status.nameLen = 4128;
status.name = new StringBuilder(4128);
getStatus(ref status);

https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype(v=vs.110).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.