使用c ++ unmanaged Dll消耗c#数组结构

时间:2015-10-18 17:13:07

标签: c# c++ arrays struct

因为我是c++ DLL世界的新手,并且在成功测试与c#进行简单任务后, 我现在正尝试使用c++消费,array通过struct [StructLayout(LayoutKind.Sequential)] public struct chars { public int V; //[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 10)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public char[] testStr; } [DllImport("ExportStructArr.dll", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] public static extern void dodata(int requestedMasterArrLength, int requestedStringSize,[MarshalAs(UnmanagedType.LPArray), Out()] chars[] Cars); //tried also this signature public static extern void dodata( int requestedMasterArrLength, int requestedStringSize, out IntPtr chars); 操纵的数据

我还尝试将struct []包装在另一个结构中以简化编组,但我没有以任何方式成功。

在给定下面的代码的情况下,处理从c ++返回的数据的正确(以及最有效的性能)方法是什么。

C#

#ifdef EXPORTSTRUCTARR_EXPORTS
#define EXPORTSTRUCTARR_API extern "C" __declspec(dllexport)
typedef struct {
    int Id;
    char *StrVal;
}Unit;


EXPORTSTRUCTARR_API  void dodata(int requestedMasterArrLength,int requestedStringSize, Unit **UntArr)
{

    int ccountr,count;
    count=0;


     *UntArr = (Unit*)LocalAlloc(0, requestedMasterArrLength * sizeof(Unit));
     Unit *CurU = *UntArr;
    while(count!= requestedMasterArrLength)
    {
        char dummyStringDataObject[]= "abcdefgHi";
        CurU[count].StrVal = NULL;

        dummyStringDataObject[0] = count+'0';
        CurU[count].Id = count;


        CurU[count].StrVal= (char*) LocalAlloc(0,(sizeof(char)* requestedStringSize));
        ccountr=0;
        while(ccountr!= requestedStringSize){
            CurU[count].StrVal[ccountr] = dummyStringDataObject[ccountr];
            ++ccountr;
        }
         ++count;

    }


}

C ++

    public class HomePage {
     WebDriver driver;  

     public static final  String PAGE_TITLE = "page title";
     public static final  String PAGE_URL = "www.blbl.pl";

@FindBy(xpath = "//*[@id='global-nav']/div/div/div[3]/ul/li[1]/a")
WebElement LogInLink;

    public HomePage(WebDriver driver){
        this.driver = driver;
    }

    public void isHomePage(){
        String pageTitle = driver.getTitle();
        Assert.assertEquals(pageTitle, PAGE_TITLE);
    }

    public void goToLoginPage(){
        LogInLink.click();
    }
}

0 个答案:

没有答案