P/Invoke Libraries for C++ Assemblies

  • Thread starter Thread starter jp2msft
  • Start date Start date
J

jp2msft

I've got a library from a government agency that only comes with the .lib and
a header (.h) file.

The header shows me what methods are exposed, but I'm not sure if I can get
to those features through a .Net Framework.

If I want to call one of the four (4) exported methods of the DLL, how would
I go about formatting the P/Invoke call to them?

Here is an example of one of the exposed methods (from the header):

extern void standardCalc(double param(), double inputs(), double outputs());

I'm getting rather used to C# syntax, but I think "double param()" is an
array of doubles - what I'd call "double[] param" in C#. If I am mistaken,
please correct me!

So, how would I go about invoking this exposed method from the library file
"aesra10.lib"?

Also, all examples of P/Invoke only list the file name, not it's location.
Do all files that I invoke this way have to be in a certain folder or can I
direct it to the folder where my application will be storing this DLL?
 
jp2msft said:
I've got a library from a government agency that only comes with the .lib and
a header (.h) file.

The header shows me what methods are exposed, but I'm not sure if I can get
to those features through a .Net Framework.

If I want to call one of the four (4) exported methods of the DLL, how would
I go about formatting the P/Invoke call to them?

Here is an example of one of the exposed methods (from the header):

extern void standardCalc(double param(), double inputs(), double outputs());

I'm getting rather used to C# syntax, but I think "double param()" is an
array of doubles - what I'd call "double[] param" in C#. If I am mistaken,
please correct me!

So, how would I go about invoking this exposed method from the library file
"aesra10.lib"?

Also, all examples of P/Invoke only list the file name, not it's location.
Do all files that I invoke this way have to be in a certain folder or can I
direct it to the folder where my application will be storing this DLL?

I'm pretty sure you can't directly access functions within the static
library file aesra10.lib.

You will first need to use C/C++ to link it into a dynamic (.dll)
library of your making. You should then be able to use P/Invoke from C#
to access its functions.

Once you've made a dll you should put it in the same folder as your program.
 
Here is an example of one of the exposed methods (from the header):

extern void standardCalc(double param(), double inputs(), double
outputs());

Did you copy and paste that directly from the header? I don't claim to be a
C++ expert but I cannot see how that can possibly be valid syntax. Arrays
would be expressed as

extern void standardCalc(double[] param, double[] inputs, double[]
outputs);

I was just recently reading a book on C++ to knock the rust off my skills
because I was thinking of writing a program under Linux and I never saw
anything even remotely similar to the syntax you have above.
 
That does not look like correct C to me. Parens are used only for
function declarations and calls when paired with an identifier (they are
also, of course, used for certain program statements and expression
grouping).

It's perfectly correct C but it doesn't mean what the OP thinks. If that
really is the declaration then it means a pointer to a function with no
parameters that returns a double result.

Cheers
Doug Forster
 
Jason Keats used his keyboard to write :
jp2msft said:
I've got a library from a government agency that only comes with the .lib
and
a header (.h) file.

The header shows me what methods are exposed, but I'm not sure if I can get
to those features through a .Net Framework.

If I want to call one of the four (4) exported methods of the DLL, how
would
I go about formatting the P/Invoke call to them?

Here is an example of one of the exposed methods (from the header):

extern void standardCalc(double param(), double inputs(), double
outputs());

I'm getting rather used to C# syntax, but I think "double param()" is an
array of doubles - what I'd call "double[] param" in C#. If I am mistaken,
please correct me!

So, how would I go about invoking this exposed method from the library file
"aesra10.lib"?

Also, all examples of P/Invoke only list the file name, not it's location.
Do all files that I invoke this way have to be in a certain folder or can I
direct it to the folder where my application will be storing this DLL?

I'm pretty sure you can't directly access functions within the static library
file aesra10.lib.

You will first need to use C/C++ to link it into a dynamic (.dll) library of
your making. You should then be able to use P/Invoke from C# to access its
functions.

Once you've made a dll you should put it in the same folder as your program.

In that case, it would be easier to just create a C++/CLI assembly:

http://tom-shelton.net/index.php/2008/12/

Then you can just reference it like any other managed assembly...
 
It's perfectly correct C but it doesn't mean what the OP thinks. If that
really is the declaration then it means a pointer to a function with no
parameters that returns a double result.

I was going to put that in my reply and then I thought, "Nah, that couldn't
be...."
 
Huh. Since when has C/C++ allowed the omission of the * in function
pointer declarations? Is that new in the last ten years or so?

You're right, the code compiles just fine with both VS and gcc. But I've
never seen function pointers declared that way in C/C++, and I can't
even find that syntax documented in the usual places I'm in the habit of
looking (for example, the MSDN page documenting pointer syntax —
http://msdn.microsoft.com/en-us/library/89e4h321(v=VS.100).aspx — only
has the * version, with no mention at all of the alternative).

I've always hated the "int (*foo)(void)" syntax; I wish I'd known I
could have always just been writing "int foo(void)" as the type! Of
course, now almost all the code I write is in C# and I don't have to
deal with C/C++'s clunky syntax. But it would've been nice "in the old
days". :)

The C89 standard says:

<quote>
To pass one function to another, one might say

int f(void);
/*...*/
g(f);

Note that f must be declared explicitly in the calling function, as
its appearance in the expression g(f) was not followed by ( . Then
the definition of g might read

g(int (*funcp)(void))
{
/*...*/ (*funcp)() /* or funcp() ... */
}

or, equivalently,

g(int func(void))
{
/*...*/ func() /* or (*func)() ... */
}

</quote>

Arne
 
Arne said:
The C89 standard says:

<quote>
[...]
or, equivalently,

g(int func(void))
{
/*...*/ func() /* or (*func)() ... */
}

</quote>

Well, I did learn C and was using function pointers before 1989. So I
guess it wasn't technically part of the established language at that time.

It may even have been working before that. C89 was the first officially
C standard.

But I don't have a copy of K&R from before 1989 to check.
But still, that second syntax is so much nicer, I'm bewildered why it
isn't more commonly used (and of course, how I managed to go all this
time without ever even seeing it).

If I were to guess then people prefer the * syntax because
we call it a function pointer.

Arne
 
Arne said:
It may even have been working before that. C89 was the first officially
C standard.

But I don't have a copy of K&R from before 1989 to check.

My 2nd edition copy, copyright 1988, makes no mention of it (yet, does
have "ANSI C" written prominently over the cover as part of the
artwork). It does discuss the "new" syntax of _calling_ functions
through a pointer in which the * is omitted, but not of declaring the
variables that way.

I agree that there were probably C compilers even before that, that
allowed the no-* syntax. It seems like a lot of "new" features in C
came about after they were first seen in one compiler or another (there
lacking a real standard). But it surely wasn't widely known.
If I were to guess then people prefer the * syntax because
we call it a function pointer.

A fine guess, except that it is very common practice to typedef function
pointers specifically so one does _not_ have to deal with the * (well,
and all the other stuff too…I admit it's not entirely just about the *).

I'm not sure there really is that much of an aversion among C
programmers to using pointer types that don't actually make one type the *.

That said, I haven't got any better guesses, so I suppose I'll just have
to remain bewildered. :)

Pete
 
A fine guess, except that it is very common practice to typedef function
pointers specifically so one does _not_ have to deal with the * (well,
and all the other stuff too…I admit it's not entirely just about the *).

But then there would typically be something in the new name that
indicates the same (ROUTINE or PROC is common in Win32 API).
That said, I haven't got any better guesses, so I suppose I'll just
have to remain bewildered. :)

Maybe someone wrote a function pointer tutorial back in the 70's or 80's
using the common syntax and lots of people learned from that and the
next generation of programmers learned from them and the next
generation again ... and so on.

Arne
 
u need:
1, pack the lib and .h file into a dll and export all the methods u want.
2, translate ur c methods into its c# equivalent with P/Invoke.

P.S. c# equivalent of extern void standardCalc(double param(), double
inputs(), double outputs()) is:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate double CallBack();
extern void standardCalc(CallBack param, CallBack inputs, CallBack outputs);

the parameter of attribute UnmanagedFunctionPointer depends on the
calling convention of ur function pointer.

ping235
 
Back
Top