1

I have a struct defined as

struct DiskInfo{
int size,
char **atributes
};

I need to sent it to c++ dll that will run something like

for(i=0;i<Files.count;i++)
{
   newList[i] = (wchar_t *)malloc(sizeof(List->Strings[i]));  
   wcscpy(newList[i], List->Strings[i]);
  .... more code
 }

At this point I have the struct poplutaed will all the data, and i need to send it back to c#. I tried:

internal struct DiskInfo
    {
        internal int size;
        [MarshalAs(UnmanagedType.ByValArray)]
        internal string[] Files;
    }

and the signature is

[DllImport("Disk.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = GetDiskInfo")]
    internal static extern uint GetDiskInfo(
       [In, Out] 
        DiskInfo Param);

And call to function

 data= new DiskInfo();
 Native.GetDiskInfo(data);

However, data is always returned as null. What am I doing wrong?

2
  • I'm confused. The C++ and C# structs shown here have different names, different member order, and the C++ version doesn't contain a "ByVal" array. Are these supposed to be the same structs? Commented Jul 15, 2011 at 9:18
  • fixed itm yes, they are the same struct Commented Jul 15, 2011 at 9:28

1 Answer 1

1

In C#, DiskInfo is a struct, which means that it is a value type, and will be passed into a method by copying (unless the ref keyword is used). So the C++ code can't possibly modify the original struct, only the copy. You might have better luck if you declared GetDiskInfo with the ref keyword and also used that keyword in your method call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.