c#pinvoke到native int *

问题描述:

我正在通过它的dll与低成本的Tucsen相机交谈。大多数命令都在工作,但有一个命令我似乎无法正确编组。 CamApi.h文件中调用的签名是:



I am talking to a low-cost Tucsen camera through it's dll. Most of the commands are working, but there is one command that I can't seem to marshal correctly. The signature of the call in the CamApi.h file is:

CAM_API int GetRoi(int *pPtX, int *pPtY, int *wid, int *hei, const char *devName);





SDK还附带了一个工作的c ++示例程序,该程序调用如下:



The SDK also came with a working c++ sample program that calls this as follows:

int Core::GetRoi(int *pPtX, int *pPtY, int *wid, int *hei, const char *devName)
{
    return (*p_GetRoi)(pPtX, pPtY, wid, hei, devName);
}



调用


which calls

p_GetRoi = (int (*)(int *, int *, int *, int *, const char *))Load_Fun("GetRoi");







我的第一个c#尝试是:




My first c# attempt was:

public int TCgetRoi(
    ref int pointx,
    ref int pointy,
    ref int wid,
    ref int hei,
    string devName)
{
    int iOut = TC_getRoi(out pointx,
        out pointy,
        out wid,
        out hei,
        devName);
    return iOut;



with


with

[DllImport("CamCore.dll", EntryPoint = "GetRoi", 
    CallingConvention = CallingConvention.StdCall)]
private static extern int TC_getRoi(
    out int pointx, out int pointy,
    out int wid,
    out int hei,
    string devName);





当我调用它时,返回int是0表示正常执行,devName也很好,但是四个整数出错了:58532,5852,8024,8276。当我运行c ++示例时,它们出现了非常合理的值:0,0,1280 ,1024。



在c#代码中,我是否正在读取一个指针,我必须再次尊重才能获得int值?



我尝试了很多其他的东西,并且总是得到相同的结果。任何人都可以看到我不能做的事情吗?



When I call it, the return int is 0 indicating a normal execution, and devName also comes out fine, but the four integers come out all wrong: 58532, 58528, 8024, 8276. When I run the c++ example they come out with very reasonable values: 0, 0, 1280, 1024.

In the c# code, am I reading a pointer that I have to deference again in order to get the int value??

I have tried many other things, and always with the same result. Can anyone see what I cannot?

Tucsen相机的人们非常乐于助人。我发给他们这个项目。他们对它进行了测试,说他们对它进行了一些修改并将其发回。它仍然给出了相同的结果,但是因为它们在发送它时它们正在为它们工作,所以我更加确信pinvoke代码一直在工作,但dll实际上是在返回疯狂的数字。我在上面的GetRoi调用之前添加了一些其他的调用,现在它正在工作,数字是合理的。



摘要:上面的代码很好沿着,相机卡在一个导致dll返回疯狂数字的状态。 Tucsen相机dll有点弱,因为它永远不会返回超出整个图像范围的数字。但图森因努力和愿意提供帮助而获得高分。特别感谢Jason Gleim,他帮助了几个想法,最终还指出了疯狂数字实际上是由dll发送的结论。
The folks at Tucsen camera were very helpful. I sent them the project. They tested it, said that they modified it a little, and sent it back. It still gave the same result, but since it was working for them when they sent it I became more confident that the pinvoke code was working all along, but the dll was actually returning crazy numbers. I added some other calls to the camera prior to the GetRoi call above, and now it is working and the numbers are reasonable.

Summary: The code above was fine all along, and the camera was stuck in a state that caused the dll to return the crazy numbers. The Tucsen camera dll is a bit weak, since it should never return numbers that are outside the range of the whole image. But Tucsen gets high marks for effort and willingness to help. Special thanks to Jason Gleim, who helped with several ideas that eventually also pointed to the conclusion that the crazy numbers were actually being sent by the dll.