开发者

string array C# Interoperability to C++ dll; string array from C# to C++ dll which sets the data and sends it back to c#

this is what i have so far... basically I use the c++ dll to populate the tree components set them and then return them to the managed csharp code. I am running into problems and have tried the entire day with no success. All I want to accomplish is to send a struct with a string 开发者_如何学JAVAarray from c# to c++ dll where it sets all the components of the tree and then sends it back to the c#. here is what i got.. the only this that has to stay is in the c++ code is the data is set from a TCHAR array.

c++ unmanged code:

 struct GetTreeStruct
 {    
     char** components;
 };

extern "C" __declspec(dllexport) void __stdcall GetTree(GetTreeStruct* myStruct);

void __stdcall GetTree(GetTreeStruct* myStruct)
{

    myStruct->components = new char *[sNumberReturned]; //sNumberReturned = some # 

    for(i = 0;i<sNumberReturned;i++)
    {
       myStruct->components[i] = (char*)(arrayItem[i]); //arrayItem is TCHAR array
    }

}

c# managed code:

public struct GetTreeStruct
{
    [MarshalAs(UnmanagedType.LPArray)]
    public string[] treeComponents;
}

[DllImport(@"C:\Users\James\Desktop\ShaggyDLL\Release\ShaggyDLL.dll")]
public static extern void GetTree(ref GetTreeStruct theStruct);

in my main..

//getTree
getTreeStruct.treeComponents = null;

Console.WriteLine("Get Tree:");
NativeMethods.GetTree(ref getTreeStruct);

list<string> list = getTreeStruct.treeComponents.ToList();

//print list
//... you know the code

any help at all would be very much appreciated. Thanks, James.


Wouldn't you need to pass 16-bit characters for C# to get it? (pun intended)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜