ssli2005

I am trying to use a C++ dll in my C# application but unsuccessul. Here is what I have done:

1. The C++ code was developed using open BSD.

2. I made a C++ CLR dll using visual studio 2005 - "tc.dll"

3. In my C# application, I add the "tc.dll" as a reference from the solution explorer

4. I tried to instantiate the class of tc.dll using

myClass mc = new myClass();

The C# program can not find the class at all.

I assumed that since it is built as a assembly dll, all the class and their member functions in there should be visible if referenced.

I am very new to C# and .Net, and not sure if what I am doing is indeed in the right direction. Please help.

Thanks a lot,

-Sue



Re: Visual C++ General Calling C++ dll from C#

cgraus

You can only import dlls if they are also written using .NET, or if they use COM. Otherwise, you can use pinvoke to call methods in the dlls.

www.pinvoke.net shows how to do this with Windows API calls, the principle is the same for any dll.






Re: Visual C++ General Calling C++ dll from C#

Nasha

Hi there,

It would be great if you could post some sample code or the exact error message that would be great.

Regards,

Nasha






Re: Visual C++ General Calling C++ dll from C#

ssli2005

OK. Here is what I am trying to do:

I have a native c++ dll, it has numbers of classes exported as following:

class DLLEXPORT Provider: public IProvider{

public:

Provider();

virtual ~Provider();

virtual int CreateSource(const String& name, const testAttributes & inAttributes, String& outAttribute);

};

class DLLEXPORT testAttributes : public IAttributes

{

public:

String NamespaceName;

Timestamp time1;

Timestamp time2;

ServiceType currentType;

String Comment; // if this is empty, the default "<CPL Title> - <Cert CommonName>" will be used

testAttributes ();

virtual ~testAttributes ();

};

Now, if I wanted to use the member function " CreateSource(const String& name, const testAttributes & inAttributes, String& outAttribute)" defined above, what should I do Just to note here one of the input params "testAttributes & inAttributes" is a class and exported from the dll.

Any help would be highly appreciated!

-sue





Re: Visual C++ General Calling C++ dll from C#

cgraus

In essence, your C++/CLI class needs to define a class to mirror the testAttributes class, which is exposed to .NET. Then, this class needs to expose all these methods, and hold an instance of the dll exported class internally. Each method on the C++/CLI class needs to pass it's method calls over to the internally held instance of the dll imported class. Then, when .NET creates an instance of this class, it will be manipulating an instance of the class you want to export.






Re: Visual C++ General Calling C++ dll from C#

ssli2005

cgraus, thanks for reply. I got your concept. I am a beginner, wondering if you could provide an example or just point me to some samples. thanks. -sue



Re: Visual C++ General Calling C++ dll from C#

cgraus

It concerns me that a lot of people who describe themselves as beginners are obviously being asked to write complex code. I'm sorry, I've used C++/CLI to wrap APIs, but not exported classes. I don't know of any examples, I just know what the concepts required are.






Re: Visual C++ General Calling C++ dll from C#

ssli2005

cgraus,thanks.

Most of the people(at least in my dev group) assume that MSFT supports the native C++ code natually and it should be very easy to integrate but obviously it is not the case. It is hard to those who don't know.





Re: Visual C++ General Calling C++ dll from C#

Ben Anderson MSFT

While it is easy for someone who knows C++/CLI, it is not easy if you do not know it or understand its concepts - much like even "Hello World" apps make no sense to non-programmers. I suggest brushing up on .Net/native interop, perhaps through some of the online C++/CLI tutorials at which point it should be relatively simple for you to write a .Net wrapper class.

For an overview of C++/CLI see:

http://msdn.microsoft.com/msdnmag/issues/06/00/PureC/default.aspx

For an overview of your interop options see:

http://msdn.microsoft.com/msdnmag/issues/04/05/VisualC2005/default.aspx






Re: Visual C++ General Calling C++ dll from C#

keerti_maverick

Hi ,

When i am trying to use the dll which i imported using DlImport it throws me an error "Specified Dll could not be found".Please take a look at the code to see the error :

/**Calling unmanaged C++ wrapper dll**/

[DllImport("TestDll.dll", EntryPoint = "AddTwoNumbers", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]

/**Declaration of function*/

/*use this function in export click event*/

static extern int AddTwoNumbers(int firstno,int secno);

public void Integrate()

{

int result = AddTwoNumbers(2, 3);

}

I have placed the c++ dll in my bin/debug folder of .NET soln. Do i have to do any specific registration of this assembly file. Any suggesstions would be of great help.

Thanks & Regards

keerti Somasundaram