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);
}
Michail
  • 366
  • 1
  • 3
  • 14
  • 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. – Hans Passant Dec 09 '14 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? – Michail Dec 09 '14 at 12:29
  • http://stackoverflow.com/a/15664100/17034 – Hans Passant Dec 09 '14 at 12:34
  • Than you. So, @8 is size of arguments. And what about AccessViolationException? How i can give to fuction memory, to write? – Michail Dec 09 '14 at 12:42
  • 1
    Why are you using PInvoke with C++/CLI? Does not make sense. – leppie Dec 09 '14 at 12:48
  • @leppie What is your solution? – Michail Dec 09 '14 at 12:49
  • 1
    In C++/CLI you should be able to call the function directly as far as I know. – leppie Dec 09 '14 at 12:53
  • @leppie Can you give more explenation about this, please? As i know in CLI(.NET) you need to make Wrapper to call native code. – Michail Dec 09 '14 at 13:00
  • Why cant you just do: `long result; cammGetCurrentBinning(index, &result)` in C++/CLI? – leppie Dec 09 '14 at 14:08
  • @leppie i hane only DLL file. How i can link name to DLL function? – Michail Dec 09 '14 at 14:18
  • What is odd here is that you show C# pinvoke. Where does that come from? – David Heffernan Dec 09 '14 at 14:42
  • There's little point asking if you don't listen. That's not how to make the address of a long. Use `&` for that. And pinvoke is totally wrong here. – David Heffernan Dec 09 '14 at 20:18
  • Pinvoke feeds my needs. It you, who is not reading. I did not asked about use it or not. And i do not need the address of the long. I need to solve access error. – Michail Dec 10 '14 at 12:02

1 Answers1

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
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • This is C++/CLI, so I am confused by all this p/invoke. Is it really needed? MC++ was fine mixing. – leppie Dec 09 '14 at 14:09
  • @leppie The pinvoke in the question is clearly C#. The question is a bit of a mess I do concede. – David Heffernan Dec 09 '14 at 14:10
  • 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 Dec 09 '14 at 14:36
  • 1
    @leppie I've updated my answer to attempt to be schizophrenic – David Heffernan Dec 09 '14 at 14:47
  • OK, but how it is better comparing PInvoke? it is more work: i need to make .lib and use LoadLibrary and GetProcAddress. What is the benefits of this? – Michail Dec 09 '14 at 15:06
  • 1
    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. – David Heffernan Dec 09 '14 at 15:07
  • Thank you for get a led answer. You mean more complexity for compiler, so longer execution time, or for coding? I think, what complex functions will be complex everywhere. – Michail Dec 09 '14 at 20:15
  • It will more complex to code, and perform worse, to use pinvoke. – David Heffernan Dec 09 '14 at 20:19
  • Agree about the perform. But instead of making .lib i only need to make pinvoke in right format. – Michail Dec 10 '14 at 12:05
  • Making a .lib file is really easy. You just build a fake dll with empty stubs for your exports. Throw away the dll and keep the lib. – David Heffernan Dec 10 '14 at 12:41
  • yes, but why to do something, if it not necessary to do it? It is really helping to solve something? And you code will not work in c++ cli in any way. It will have the another syntax. :) – Michail Dec 10 '14 at 13:24
  • It is handy for my in global scope of the project. But it not the main point. The question is "why i can not use 1 line solution"? It is so bad? Your code in this answer will cause compiler error in c++ cli. – Michail Dec 10 '14 at 13:42
  • You can do whatever you like. Including allocating a 4 byte integer on the heap instead of using `&` with a local variable. It's entirely up to you what you do. – David Heffernan Dec 10 '14 at 13:43
  • It was not my dream to allocate bytes. The problem, what by using '&' i have AccessViolationException. It is my main question it this topic. – Michail Dec 10 '14 at 13:49
  • If that is so, and that really is the issue, then where is the code? – David Heffernan Dec 10 '14 at 13:50
  • The line "static int Get_Current_Binning(int index, out int result);" will cause "error C2061: syntax error : identifier 'ref'". It is because of anohter sintax of c++ cli conmaring c#. Not important, becouse i am not using you code in any way. I have another question: how to replace memory allocation, if it is possible? – Michail Dec 10 '14 at 13:55
  • No, that code won't lead to a syntax error since it is C# code. Remember that there has been confusion over what language is being used. Declare a local variable and take its address using the `&` operator. – David Heffernan Dec 10 '14 at 13:58
  • Thank you! You did push me to investigation and i just found the solution. Not something i will publish here. This question is in one step to delete. One more: you should not have confusion, because it was c++ in Tags. – Michail Dec 10 '14 at 14:32