i am importing c++ function from a dll to my winform c# app:
[DllImport(@"eyeWhere.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern int Eye_GetPositionS(string filename, [MarshalAs(UnmanagedType.LPArray, SizeConst = 9)] double[] sensors);
when i call the function from the constructor its working fine.
the problem is when i am calling it from a new thread opened within the
"fleck websocket server" Fleck , "onMessage" Action,
then it throws the "system.access.violation" exception.
i managed to narrow down the problem to the double array that i am passing, it seems like the pointer to it is corrupted.
i cant find the source of the problem one thing is sure the function from the dll is working as i tested it.
function call(two stages):
socket.OnMessage = message => {Thread locationThread = new Thread( unused => processLocation(fileName,socket,sensorsList,sensors) ); locationThread.Start();}
private void processLocation(string fileName, IWebSocketConnection sock, List<Sensor> sensorsList, double[] sensors) { int map_position = Eye_GetPositionS(fileName,sensors);
string locationString = "floor:1,mx:" + (map_position / 10000) + ",my:" + (map_position % 10000); // send location string to user sock.Send(LOCATION_CODE + "-" + locationString);}
the interface is:
extern "C" __declspec(dllexport) int Eye_GetPositionS(const wchar_t *fname_mob, double sensors[9], int &map_x, int &map_y)
i am not passing the two last arguments (int&) as agreed with the man who wrote the dll.
I am not passing the two last arguments (
int&
) as agreed with the man who wrote the DLL.
Well, there's the problem. You cannot omit parameters. It should be:
[DllImport(@"eyeWhere.dll", CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode)]
public static extern int Eye_GetPositionS(
string filename,
[In, MarshalAs(UnmanagedType.LPArray, SizeConst = 9)]
double[] sensors,
ref int map_x,
ref int map_y
);
You simply cannot omit these parameters, no matter what the guy who wrote the DLL says. Perhaps he means that the parameters are really int*
and you can pass nullptr
.
I've used ref
for these parameters, but perhaps they should be out
. You presumably know which is appropriate.
Likewise I'm guessing at the intent of the sensors
parameter. If the data flows out rather than in, then use Out
. If the data flows in both directions, use [In, Out, ...]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.