Michail
Michail

Reputation: 366

Passing pointer as argument to native function with PInvoke

I need to call native function

long (WINAPI*)(long,long*);

In long* it will give me the result

i am doing this

[DllImport("mrfw.dll", EntryPoint = "_McammGetCurrentBinning@8")]
static long Get_Current_Binning(Int32, IntPtr);

but this call is not working

Int32 Camera_Index= 0;
Int32 Result;
IntPtr Result_Pointer = IntPtr(Result);

long Binning = Get_Current_Binning(Camera_Index, Result_Pointer);

I have the exception System.AccessViolationException So, function can not write me the result.

How to do this?

Thank you.


Update

Hey guys. I do not know what you all mean, but i did ask, what i did ask.

-I am using c++ cli. it is not c#

-I need pinvoke. I can not call unmanaget dll from cli project

-i did found the solution by doing this. You can delete my answers, give me the minuses, but it is working. It is so bad?

[DllImport("mrfw.dll", EntryPoint = "_McammGetCurrentBinning@8")]
static long Get_Current_Binning(Int32, IntPtr);

int main(array<System::String ^> ^args)
{
 Int32 Camera_Index= 0;
 Byte* Result= new Byte(4);
 IntPtr Result_Pointer = IntPtr(Result);

 long Binning = Get_Current_Binning(Camera_Index, Result_Pointer);
}

Upvotes: 0

Views: 305

Answers (1)

David Heffernan
David Heffernan

Reputation: 612804

You are coding in mixed mode C++/CLI and there's no need for p/invoke. If what you have is a DLL without a .lib file, and a mixed mode C++/CLI program, then you have a couple of options.

  1. Obtain or create a .lib file for the DLL. Then link to the DLL using implicit linking in the traditional way.
  2. Link to the DLL using explicit linking with LoadLibrary and GetProcAddress.

In either case you will be able to call the function like this:

long result;
long retval = Get_Current_Binning(0, &result);
// check retval for success, and if so the result contains the returned value

Upvotes: 1

Related Questions