0

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);
}
13
  • The entrypoint name says that the function has one argument, not two. Visible from the @4 postfix. You are working from with bad documentation or just guessed wrong at the proper function pointer type for this function. You could just try omitting the 2nd parameter as a wild guess, it certainly is never an IntPtr. Contact the vendor for support. Commented Dec 9, 2014 at 12:00
  • @Hans Passant thanks to you i did fix the problem, but next did come. And one more question for you: how i can learn to read entery pont name? Commented Dec 9, 2014 at 12:29
  • stackoverflow.com/a/15664100/17034 Commented Dec 9, 2014 at 12:34
  • 1
    Why are you using PInvoke with C++/CLI? Does not make sense. Commented Dec 9, 2014 at 12:48
  • 1
    In C++/CLI you should be able to call the function directly as far as I know. Commented Dec 9, 2014 at 12:53

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

18 Comments

This is C++/CLI, so I am confused by all this p/invoke. Is it really needed? MC++ was fine mixing.
@leppie The pinvoke in the question is clearly C#. The question is a bit of a mess I do concede.
That's what I thought too, but tagged and the rest of the syntax looked a bit un C# like (his answer at least) :)
@leppie I've updated my answer to attempt to be schizophrenic
You are just adding a layer of complexity by using p/invoke, one that you don't need. It will also get rapidly more complex to use p/invoke when you need more complex functions. Indeed when faced with complexity people often switch to C++/CLI.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.