Call C++ function in C# from DLL - strange parameters

穿精又带淫゛_ 提交于 2019-12-06 11:12:35

Marshalling works best with C Style calls. So it is best to use pure C on your public interface. If it is at all feasible to change the native code to

void EncodeFromBufferIN(
    unsigned char* bufferIN, 
    int bufferINSize, 
    unsigned char* bufferOUT,     
    int* bufferOUTSize);

Then the call in C# can be thus

[DllImport("QASEncoder.dll")]
public static extern void EncodeFromBufferIN(
    String bufferIN, 
    int bufferINSize, 
    StringBuilder bufferOUT, 
    ref int bufferOUTSize);

String inStr = new String(255);
int inSize = 255;
// make an educated estimate for the output size 
// and preallocate in C# (I am guessing 255)
StringBuilder outStr = new StringBuilder(255);
int outSize = 255;

EncodeFromBufferIN(inStr, inSize, outStr, outSize);

This way you can avoid memory allocations in unmanaged code which (although feasible) can get messy.

Hope this gets you going.

A few corrections that worked for me:

        string inStr = "Value to pass";
        int inSize = inStr.Length;
        StringBuilder outStr = new StringBuilder(255);
        int outSize = 255;

        EncodeFromBufferIN(inStr, inSize, outStr, ref outSize);

regards.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!