Noobie and Newbie

Hi guys,

Can anyone guide me how to create a wrapper

Basically I have a file Object library and a header file from the vendor which is written in eVC++. My application is written in C#.net and my objective/goal is to create a wrapper for the C++. Is there any guide or reference to refer to create a wrapper and call in my c# application


Thanks..



Re: Visual C++ General Wrapper

einaros

I think http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/ sums it up pretty good. Have a look at that, and tell us if anything's unclear to you.





Re: Visual C++ General Wrapper

Noobie and Newbie

Hi einaros,

Thanks for your fast reply,

Please correct and guide me if I am wrong as things start to get confusing (too many questionmark in my head),

From what I understand that is using PInvoke (Platform Invoke) right.

But I found an URL how to create wrapper is something like the below

http://www.ondotnet.com/pub/a/dotnet/2004/03/29/mcpp_part3.html page=1

What is the different between them, PInvoke way and create a wrapper way to get call by C# application.

Thanks,

Regards,

Jimmy






Re: Visual C++ General Wrapper

einaros

Both articles are quite old. In either case, P/Invoke will play a key part no matter how you go about this. Your options for a solution pretty much limit themselves to 1. writing a managed app which imports specific functions, or 2. writing a mixed C++/CLI appilication, which includes the headers and links the import library of a C++ DLL library. In both cases, you'd re-expose the functionality from the dll through your own wrapper class.





Re: Visual C++ General Wrapper

Noobie and Newbie

Hi einaros,

Thanks for your help, now I seem to have must clearer view.

Anyway, I went to bought a book title call Pro Visual C++/CLI and the .NET 2.0 Platform by Apress for self-learning.

Any more recommended books if I want to learn more in depth.

Regards,

Jimmy.






Re: Visual C++ General Wrapper

Aleksandr Tokarev

Lets a vendor gives you a Class1.dll, Class1.h, Class1.lib

#ifdef CLASS1_EXPORTS

#define CLASS1_API __declspec(dllexport)

#else

#define CLASS1_API __declspec(dllimport)

#endif

// This class is exported from the Class1.dll

class CLASS1_API CClass1 {

public:

CClass1(void);

void M1();

};

// This is the constructor of a class that has been exported.

// see Class1.h for the class definition

CClass1::CClass1()

{

return;

}

void CClass1::M1()

{

printf(__FUNCTION__ "\n");

}

You need to create a C++ Class Library project - ClassLib1.

This class lib containes Class1:

#pragma once

#include "..\class1\class1.h"

using namespace System;

namespace ClassLib1 {

public ref class Class1

{

private:

CClass1& m_cl1;

public:

Class1()

: m_cl1(*new CClass1)

{

}

~Class1()

{

delete &m_cl1;

}

void M1()

{

m_cl1.M1();

}

};

}

add a refference to you Class1.lib using

ClassLib1 properties -> Configuration properties->Linker->Input

Additional Dependences: $(NoInherit); "[LibPath]Class1.lib"

Or using

#pragma comment(lib, "[LibPath]Class1.lib")

Compile! Should be compiled!

Then create C# project.

Add a reference to you ClassLib1.

static void Main(string[] args)

{

ClassLib1.Class1 cl = new ClassLib1.Class1();

cl.M1();

}

Make sure that Class1.dll, ClassLib1.dll lies beside your C# .exe program! By default ClassLib1.dll copied to output folder but not its dependences! It meand that you need manually copy Class1.dll.

If everything is right then you will obtain this output:

CClass1::M1
Press any key to continue . . .





Re: Visual C++ General Wrapper

Noobie and Newbie

Hi Aleksandr Tokarev,

Thanks for your help

I have try the above coding. It works very well in the console application, but it does not work in device application as it give me a error of

An unhandled exception of type 'System.MissingMethodException' occurred in WinCE.exe

when execute the exe file

Any ideas


Regards,
Jimmy






Re: Visual C++ General Wrapper

Aleksandr Tokarev

Where does this exception occuried Can you debug it





Re: Visual C++ General Wrapper

Noobie and Newbie

Hi Aleksandr Tokarev,

This is the line that cause the problem -->

ClassLib1.Class1 cl = new ClassLib1.Class1();


For me,

Manage.ClassWrap x = new Manage.ClassWrap(); --> An unhandled exception of type 'System.MissingMethodException' occurred in WinCE.exe


Output -->
A first chance exception of type 'System.MissingMethodException' occurred in DeviceApplication1.exe
An unhandled exception of type 'System.MissingMethodException' occurred in DeviceApplication1.exe


More Information.

C# device application --> WinCE5.0


Thanks for your help Aleksandr Tokarev.



Regards,
Jimmy





Re: Visual C++ General Wrapper

Aleksandr Tokarev

Can you post your code





Re: Visual C++ General Wrapper

Aleksandr Tokarev

Can you post your code

Catch this exception and post output that returns by Exception.ToString





Re: Visual C++ General Wrapper

Noobie and Newbie

Device application code

private void button1_Click(object sender, EventArgs e)
{
Manage.ClassWrap x = new Manage.ClassWrap();
int y = x.Test1();
MessageBox.Show("Test " + y);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Class Library code

namespace Manage
{
public ref class ClassWrap
{
// TODO: Add your methods for this class here.
private:
CSample1& m_sam;

public:
ClassWrap(): m_sam(*new CSample1)
{

}

~ClassWrap()
{
delete &m_sam;
}

public:
int Test1()
{
int x = m_sam.Sample();
return x;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Sample1.dll, Sample1.h, Sample1.lib

Sample1.h
class SAMPLE1_API CSample1
{
public:
CSample1(void);
// TODO: add your methods here.
public:
int Sample(void);
};

Sample1.ccp
CSample1::CSample1()
{
return;
}

int CSample1:Tongue Tiedample(void)
{
return 30;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

If you don't mind, I can send the three files to your email



Thanks


Regards,
Jimmy








Re: Visual C++ General Wrapper

Noobie and Newbie

Catch this exception and post output that returns by Exception.ToString


For me, I just return an Integer value only



Thanks,


Regards,
Jimmy





Re: Visual C++ General Wrapper

Aleksandr Tokarev

It's not clear.

Show me output of exception.





Re: Visual C++ General Wrapper

Noobie and Newbie

Output of the device application

'DeviceApplication1.exe' (Managed): Loaded 'mscorlib.dll', No symbols loaded.
'DeviceApplication1.exe' (Managed): Loaded 'c:\documents and settings\jimmy chan\desktop\deviceapplication1\deviceapplication1\bin\release\DeviceApplication1.exe', Symbols loaded.
'DeviceApplication1.exe' (Managed): Loaded 'System.Windows.Forms.dll', No symbols loaded.
'DeviceApplication1.exe' (Managed): Loaded 'System.dll', No symbols loaded.
'DeviceApplication1.exe' (Managed): Loaded 'System.Drawing.dll', No symbols loaded.
'DeviceApplication1.exe' (Managed): Loaded 'c:\documents and settings\jimmy chan\desktop\deviceapplication1\deviceapplication1\bin\release\Manage.dll', Symbols loaded.
A first chance exception of type 'System.MissingMethodException' occurred in DeviceApplication1.exe <--- Error Start Here
An unhandled exception of type 'System.MissingMethodException' occurred in DeviceApplication1.exe

A first chance exception of type 'System.MissingMethodException' occurred in System.Windows.Forms.dll
An unhandled exception of type 'System.MissingMethodException' occurred in System.Windows.Forms.dll

The thread 0x8b93a656 has exited with code 0 (0x0).
The thread 0xebab768a has exited with code 0 (0x0).
The program '[8bd65d36] DeviceApplication1.exe: Managed' has exited with code 0 (0x0).



Thanks

More Information

I am doing in the release mode.


Regards,
Jimmy