Hi i've go a procedure like this:

c++
esession->Create_message(p1,p2);

I need to translate in p/invoke

Thank's

Re: p/invoke by Robert

Robert
Thu Apr 27 14:55:24 CDT 2006

1. Do you have the source of the C++ program? Can you modify it?
2. What is the return type and parameter types of the function?
3. Is this a static method on a C++ class or a member function?
4. Is this a C++ COM object?

I'll assume for sake of argument that:
1. You do have the source code and are allowed to modify it
2. Arguments aren't that important for illustration purposes
3. It's a member function
4. It's not a COM object.

In order to call a function in a C++ library it must be a static method
exported by the DLL.

Your code appears to be a member function of a C++ class. You cannot
directly call such an object from C#.

In order to make that work, you'll need three exported functions. One to
make an instance of the object and return it as an IntPtr, a second static
function that takes the IntPtr and the p1 and p2 variables, and calls the
member function for you -- and a 3rd to destroy the object.

For example:

// C++ stuff
class A
{
public:
void HelloWorld(int someVariable)
{
MessageBox(NULL, "Hello World", NULL, 0);
}
};

// Exported function to make an instance of class A
extern "C" void * WINAPI __declspec(dllexport) CreateClassA()
{
return new A();
}

// Exported function to call a member of class A
extern "C" void WINAPI __declspec(dllexport) ClassAHelloWorld(A *apointer,
int someVariable)
{
apointer->HelloWorld(someVariable);
}

extern "C" void WINAPI __declspec(dllexport) DestroyClassA(A *apointer)
{
delete apointer
}
// -- End of C++ stuff

Now to make the calls from C#, you'd do this:

// C# stuff
[DllImport("mydll.dll")]
static extern IntPtr CreateClassA();

[DllImport("mydll.dll")]
static extern void ClassAHelloWorld(IntPtr apointer, int someVariable);

[DllImport("mydll.dll")]
static extern void DestroyClassA(IntPtr apointer);

void Main()
{
IntPtr a = CreateClassA();

ClassAHelloWorld(a, 12345);

DestroyClassA(a);
}


"Agnoletto Christian" <info@actisoft.it> wrote in message
news:Ys84g.99650$A83.2320211@twister1.libero.it...
> Hi i've go a procedure like this:
>
> c++
> esession->Create_message(p1,p2);
>
> I need to translate in p/invoke
>
> Thank's
>
>



Re: p/invoke by Agnoletto

Agnoletto
Thu Apr 27 15:35:29 CDT 2006


If you look my message: c++ to c# there is the source that i'm trying to
modify.

"Robert Simpson" <rmsimpson@noemail.noemail> ha scritto nel messaggio
news:OtrL5RjaGHA.1200@TK2MSFTNGP03.phx.gbl...
> 1. Do you have the source of the C++ program? Can you modify it?
> 2. What is the return type and parameter types of the function?
> 3. Is this a static method on a C++ class or a member function?
> 4. Is this a C++ COM object?
>
> I'll assume for sake of argument that:
> 1. You do have the source code and are allowed to modify it
> 2. Arguments aren't that important for illustration purposes
> 3. It's a member function
> 4. It's not a COM object.
>
> In order to call a function in a C++ library it must be a static method
> exported by the DLL.
>
> Your code appears to be a member function of a C++ class. You cannot
> directly call such an object from C#.
>
> In order to make that work, you'll need three exported functions. One to
> make an instance of the object and return it as an IntPtr, a second static
> function that takes the IntPtr and the p1 and p2 variables, and calls the
> member function for you -- and a 3rd to destroy the object.
>
> For example:
>
> // C++ stuff
> class A
> {
> public:
> void HelloWorld(int someVariable)
> {
> MessageBox(NULL, "Hello World", NULL, 0);
> }
> };
>
> // Exported function to make an instance of class A
> extern "C" void * WINAPI __declspec(dllexport) CreateClassA()
> {
> return new A();
> }
>
> // Exported function to call a member of class A
> extern "C" void WINAPI __declspec(dllexport) ClassAHelloWorld(A *apointer,
> int someVariable)
> {
> apointer->HelloWorld(someVariable);
> }
>
> extern "C" void WINAPI __declspec(dllexport) DestroyClassA(A *apointer)
> {
> delete apointer
> }
> // -- End of C++ stuff
>
> Now to make the calls from C#, you'd do this:
>
> // C# stuff
> [DllImport("mydll.dll")]
> static extern IntPtr CreateClassA();
>
> [DllImport("mydll.dll")]
> static extern void ClassAHelloWorld(IntPtr apointer, int someVariable);
>
> [DllImport("mydll.dll")]
> static extern void DestroyClassA(IntPtr apointer);
>
> void Main()
> {
> IntPtr a = CreateClassA();
>
> ClassAHelloWorld(a, 12345);
>
> DestroyClassA(a);
> }
>
>
> "Agnoletto Christian" <info@actisoft.it> wrote in message
> news:Ys84g.99650$A83.2320211@twister1.libero.it...
>> Hi i've go a procedure like this:
>>
>> c++
>> esession->Create_message(p1,p2);
>>
>> I need to translate in p/invoke
>>
>> Thank's
>>
>>
>
>



Re: p/invoke by Robert

Robert
Thu Apr 27 15:53:11 CDT 2006

I just had a look at that. My sample below showed you a simple case of how
to create and destroy a C++ class and how to call a member of the class.
You'll have to apply that on a larger scale against your codebase in order
to make it work. Unfortunately if you're not sure what you're doing or how
to do it, you may be better off hiring a consultant.

Robert




"Agnoletto Christian" <info@actisoft.it> wrote in message
news:lW94g.100010$PR2.1529362@twister2.libero.it...
>
> If you look my message: c++ to c# there is the source that i'm trying to
> modify.
>
> "Robert Simpson" <rmsimpson@noemail.noemail> ha scritto nel messaggio
> news:OtrL5RjaGHA.1200@TK2MSFTNGP03.phx.gbl...
>> 1. Do you have the source of the C++ program? Can you modify it?
>> 2. What is the return type and parameter types of the function?
>> 3. Is this a static method on a C++ class or a member function?
>> 4. Is this a C++ COM object?
>>
>> I'll assume for sake of argument that:
>> 1. You do have the source code and are allowed to modify it
>> 2. Arguments aren't that important for illustration purposes
>> 3. It's a member function
>> 4. It's not a COM object.
>>
>> In order to call a function in a C++ library it must be a static method
>> exported by the DLL.
>>
>> Your code appears to be a member function of a C++ class. You cannot
>> directly call such an object from C#.
>>
>> In order to make that work, you'll need three exported functions. One to
>> make an instance of the object and return it as an IntPtr, a second
>> static function that takes the IntPtr and the p1 and p2 variables, and
>> calls the member function for you -- and a 3rd to destroy the object.
>>
>> For example:
>>
>> // C++ stuff
>> class A
>> {
>> public:
>> void HelloWorld(int someVariable)
>> {
>> MessageBox(NULL, "Hello World", NULL, 0);
>> }
>> };
>>
>> // Exported function to make an instance of class A
>> extern "C" void * WINAPI __declspec(dllexport) CreateClassA()
>> {
>> return new A();
>> }
>>
>> // Exported function to call a member of class A
>> extern "C" void WINAPI __declspec(dllexport) ClassAHelloWorld(A
>> *apointer, int someVariable)
>> {
>> apointer->HelloWorld(someVariable);
>> }
>>
>> extern "C" void WINAPI __declspec(dllexport) DestroyClassA(A *apointer)
>> {
>> delete apointer
>> }
>> // -- End of C++ stuff
>>
>> Now to make the calls from C#, you'd do this:
>>
>> // C# stuff
>> [DllImport("mydll.dll")]
>> static extern IntPtr CreateClassA();
>>
>> [DllImport("mydll.dll")]
>> static extern void ClassAHelloWorld(IntPtr apointer, int someVariable);
>>
>> [DllImport("mydll.dll")]
>> static extern void DestroyClassA(IntPtr apointer);
>>
>> void Main()
>> {
>> IntPtr a = CreateClassA();
>>
>> ClassAHelloWorld(a, 12345);
>>
>> DestroyClassA(a);
>> }
>>
>>
>> "Agnoletto Christian" <info@actisoft.it> wrote in message
>> news:Ys84g.99650$A83.2320211@twister1.libero.it...
>>> Hi i've go a procedure like this:
>>>
>>> c++
>>> esession->Create_message(p1,p2);
>>>
>>> I need to translate in p/invoke
>>>
>>> Thank's
>>>
>>>
>>
>>
>
>