c# - 将数组从 C++ 移动到 C#、修改它并将其传递回 C++ 的最简单方法

标签 c# c++ com interop

我有一个 C# 类库,其中包含需要与外部应用程序一起使用的方法。不幸的是,此外部应用程序仅支持 C/C++ 中的外部 API。

现在,我已经设法获得了一个在 C++ dll 和 C# DLL 之间工作的非常简单的 COM 示例,但我对如何移动数组数据一筹莫展。

这就是我到目前为止所得到的,就像我在网络上找到的一个通过 COM 进行通信的小例子:

DLL_EXPORT(void) runAddTest(int add1,long *result) {
    // Initialize COM.
    HRESULT hr = CoInitialize(NULL);

    // Create the interface pointer.
    IUnitModelPtr pIUnit(__uuidof(UnitModel));

    long lResult = 0;

    // Call the Add method.
    pIUnit->Add(5, 10, &lResult);

    *result = lResult;

    // Uninitialize COM.
    CoUninitialize();

}

这可以很好地调用我的 C# 类中的添加方法。我如何修改它以获取并返回一个 double 组? (我还需要用字符串来完成)。

我需要获取一个非托管数组,将此数组传递给 C# 类进行一些计算,然后将结果传递回原始函数调用(非托管)C++ 中指定的数组引用。

我需要公开这样一个函数:


*calcin - 对 double 组的引用

*calcOut - 对 double 组的引用

numIN - 输入数组大小的值

DLL_EXPORT(void) doCalc(double *calcIn, int numIn, double *calcOut)
{
      //pass the calcIn array to C# class for the calcuations

      //get the values back from my C# class

      //put the values from the C# class 
      //into the array ref specified by the *calcOut reference 


}

认为我可以为外部应用程序使用 C++\CLI DLL,所以如果这比直接 COM 更容易,那么我愿意考虑一下。

请保持温和,因为我主要是一名 C# 开发人员,但一直深陷 Interop 和 C++ 的深渊。

最佳答案

我不久前对此进行了试验,但不幸的是忘记了它们是如何组合在一起的……为了我的目的,结果证明它非常慢,所以我去掉了 C# 并回到了所有 C++。当您说您主要是一名 C# 开发人员时,我希望您理解指针,因为如果您不理解指针,就没有办法保持温和。

传递数组基本上归结为在 C++ 端使用 CoTaskMemAlloc 函数系列 (http://msdn.microsoft.com/en-us/library/ms692727(VS.85).aspx) 和在 C# 端使用 Marshal 类(http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx - 它具有类似 AllocCoTaskMem 的方法)。对于 C#,我最终得到了一个实用程序类:

public class serviceUtils
{
    unsafe public long stringToCoTaskPtr( ref str thestring )
    {
        return (long)Marshal.StringToCoTaskMemAnsi(thestring.theString).ToPointer();//TODO : what errors occur from here? handle them
    }

    unsafe public long bytesToCoTaskPtr( ref bytes thebytes, ref short byteCnt)
    {
        byteCnt = (short)thebytes.theArray.Length;
        IntPtr tmpptr = new IntPtr();
        tmpptr = Marshal.AllocCoTaskMem(byteCnt);
        Marshal.Copy(thebytes.theArray, 0, tmpptr, byteCnt);
        return (long)tmpptr.ToPointer();
    }

    public void freeCoTaskMemPtr(long ptr)
    {
        Marshal.FreeCoTaskMem(new IntPtr(ptr));//TODO : errors from here?
    }

    public string coTaskPtrToString(long theptr)
    {
        return Marshal.PtrToStringAnsi(new IntPtr(theptr));
    }

    public byte[] coTaskPtrToBytes(long theptr, short thelen)
    {
        byte[] tmpbytes = new byte[thelen];
        Marshal.Copy(new IntPtr(theptr), tmpbytes, 0, thelen);
        return tmpbytes;
    }
}

只是为了向您抛出更多代码: 这个 C++

#import "..\COMClient\bin\Debug\COMClient.tlb" named_guids raw_interfaces_only
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);   //Initialize all COM Components
COMClient::IComCalculatorPtr pCalc;
// CreateInstance parameters
HRESULT hRes = pCalc.CreateInstance(COMClient::CLSID_ComCalculator);
if (hRes == S_OK) {
    long size = 5;
    LPVOID ptr = CoTaskMemAlloc( size );
    if( ptr != NULL )
    {
        memcpy( ptr, "12345", size );
        short ans = 0;
        pCalc->changeBytes( (__int64*)&ptr, &size, &ans );
        CoTaskMemFree(ptr);
    }
}

CoUninitialize ();   //DeInitialize all COM Components

return 0;
}

调用这个 c#

    public short changeBytes(ref long ptr, ref int arraysize)
    {
        try
        {
            IntPtr interopPtr = new IntPtr(ptr);                
            testservice.ByteArray bytes = new testservice.ByteArray();
            byte[] somebytes = new byte[arraysize];
            Marshal.Copy(interopPtr, somebytes, 0, arraysize);
            bytes.theArray = somebytes;

            CalculatorClient client = generateClient();
            client.takeArray(ref bytes);
            client.Close();
            if (arraysize < bytes.theArray.Length)
            {
                interopPtr = Marshal.ReAllocCoTaskMem(interopPtr, bytes.theArray.Length);//TODO : throws an exception if fails... deal with it
            }
            Marshal.Copy(bytes.theArray, 0, interopPtr, bytes.theArray.Length);
            ptr = interopPtr.ToInt64();

            arraysize = bytes.theArray.Length;

            //TODO : do we need to free IntPtr? check all code for memory leaks... check for successful allocation
        }
        catch(Exception e)
        {
            return 3;
        }

        return 2;
    }

抱歉,但我没有时间解决所有这些问题并正确解释它,希望这会为您提供正确方向的指示,至少是一些可以谷歌搜索的内容。祝你好运

PS:我从网上得到了写这些东西的所有信息,所以它就在那里。

关于c# - 将数组从 C++ 移动到 C#、修改它并将其传递回 C++ 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/575455/

相关文章:

c++ - 如何强制 GCC 以线性方式转换 volatile 内联汇编语句?

c++ - CUDA 非原子写入冲突结果

javascript - 如何在 C++ 中构建可以使用 Javascript 编写脚本的 ActiveX 对象

c++ - 在 CoCreateInstance 之前调用 QueryInterface?

c++ - 在 dllmain()/DLL_PROCESS_ATTACH 中允许实例化 com 对象?

c# - 了解 Autofac 中间件与 ASP.NET Webforms、OWIN 和 ASP.NET Identity 的使用

c# - Moq - 是否可以在不使用 It.IsAny 的情况下设置模拟

c# - Datagridview 获取所有行的值

c# - 属性的算术运算

C++如何在.exe中编译dll