hailing

I reference a dll library which is compiled by vc++ of visual studio 2005. There is a function in the dll:

LRESULT DeviceOpen(Int32 devNum, LONG_PTR* devHandle).

When I reference this dll in a c# project, this function is showed in Object Browser:

DeviceOpen(int, long*)

I use the funcion in the c# project like:

int devNum;

long devHandle; // I compiled to 64bit application

DeviceOpen(devNum, ref devHandle)

Error occured when I compile this project: Argument '2': cannot convert from 'ref long' to 'long*'

Any help will be appreciated.



Re: Visual C# General reference a vc dll library in c# project

Bulldog.NET

Hi hailing

I seems like the C++ Method you are trying to Invoke it accepts int and long pointer data types and you have only pass the long data type not long* which I assume its an array of long numbers.

(Error occured when I compile this project: Argument '2': cannot convert from 'ref long' to 'long*' )

I order to user pointers in C# you must enable Allow unsafe code:- In the properties page of your project, there should be an option under build called "Allow unsafe code", check that.

Your method should include unsafe so that pointers can be used.

Code Snippet

unsafe public void InvokeDeviceOpen(int devNum, long[] devHandle)

{

fixed (long* inputHandle = devHandle)

{

DeviceOpen(devNum, inputHandle);

}

}

Hope this helps.






Re: Visual C# General reference a vc dll library in c# project

hailing

First, thanks for your help.

Function DeviceOpen(int devNum, LONG_PTR* devHandle) is used to get a LONG_PTR device handle, parameter devHandle is a out parameter, so, I use pointer here. Using LONG_PTR is because I compile the dll code to 32 bit and 64 bit binary.

I hope this function looks like DeviceOpen(int, ref long devHandle) in c# Object Browser, but it doesn't. Why

I have another function in dll library GetFifoSize(LONG_PTR devHandle, Int32* length), it looks like GetFifoSize(long devHandle, ref int length) in c# dll library.





Re: Visual C# General reference a vc dll library in c# project

Bulldog.NET

Hi hailing

have you tried the following:-

Code Snippet

unsafe public void InvokeDeviceOpen()

{

int devNum = 1212;//any int number

long* devHandle = null;

DeviceOpen(devNum, devHandle);

//Show me the value of devHandle

}

Huh.






Re: Visual C# General reference a vc dll library in c# project

Mattias Sjogren

Since you can reference the DLL it must be compiled to managed code. In tahta case, why don't you change the function signature to something a bit mroe friendly to C# clients, such as

IntPtr DeviceOpen(int devNum)






Re: Visual C# General reference a vc dll library in c# project

hailing

I modify the funciton to DeviceOpen(Int32 devNum, LONG_PTR __gc * devHandle), then everything is ok. And this function looks like DeviceOpen(Int32 devNum, long ref devHandle) in c# Object Browser.