Accessing c++ dll plugin in c#

G

Guest

I have an application written in c#. I have a c++ wrapper class dll plugIn. I
would like to integrate my c# application with the dll plugin and use the
fuctions written in c++ in my c# code. I need to know how to accessing c++
dll plugin using c#. Can any one give me information and suggestions.

Thanks in advance
 
G

Guest

I am afraid I am not an expert in this, but here is how I got it to work in
one of our little applications....

C#:
class Program
{
// ready the validation DLL for use
[DllImport("DocVal.dll")]
public static extern String DllMain(string path);


static void Main(string[] args)
{
try
{
// lannch our C++ DLL with the fully qualified path to the
document
String response =
DllMain(@"\\share\SharedDocs\test\Sometext.txt");
Console.WriteLine("Validation response is: " + response);
}
catch (Exception exec)
{
Console.WriteLine("There was an internal error with the DLL
:-( Details: " + exec.Message);
}
}
-----------------------
in C++: ( I choose to make a new dll project in the editor)
......normal header stuff ....

#pragma data_seg("SharedBlockOMemory")
// shared stuff here... (if needed)
#pragma data_seg()

// these are the functions we can call outside of our DLL if need be
extern LPCSTR _declspec(dllexport) DllMain(char * pathToFile);


LPCSTR _declspec(dllexport) DllMain(char * pathToFile)
{
char inputFile[255]; // our input .txt file
bool verboseMode = false; // to keep track if we want everything
logged verbosely
bool ignoreFileExtension = false; // to see if we are to ignore the file
extension



sprintf(inputFile, "%s\0", pathToFile); // and store it into a char[]

inputFileCString = pathToFile; // assign this to a CString for use later



// ... your work here...

return /*(something in LPCSTR format)*/ "Hello!";


} // end DllMain ()

Note that you may have trouble using CStrings in your C++ code... I had to
take almost all of them out when I converted some of our stuff to dlls...
expecially CString return types from calling any functions within your C
code. I used char *'s and that seemed to work well.

Hope this gets you started...

Rob K
 
G

Guest

Thank you for your reply. But the problem is I am using third party plugin. I
must use functions of a COM-interface that the plugin provides me. I think
now my problem is more clear to you. Do you have any solutions??
 
G

Guest

Hmm... I am not sure then. The only other kind of dll stuff I have done is
calling a managed dll that calls an unmanaged dll (written in C++).. but all
that required is a call to\a namespace defined in the managed dll.

I hope someone else here can help. This is the limit of my experience in
this area.

Best of Luck,

Rob K
 
J

Jianwei Sun

If it's a COM dll, then you are looking into Interop.

You need to add a reference to that COM dll.

At the top of the using, include the com interface like this:


using System.Runtime.InteropServices;
using *** YourCOM ***

Then, all the COM interface should be visible to you.

The code is like this :

m_oComObject = new ComObjectClass();


After finishing, do this:
Marshal.ReleaseComObject(m_m_oComObject);


HTH

Jianwei
Thank you for your reply. But the problem is I am using third party plugin. I
must use functions of a COM-interface that the plugin provides me. I think
now my problem is more clear to you. Do you have any solutions??


RobKinney1 said:
I am afraid I am not an expert in this, but here is how I got it to work in
one of our little applications....

C#:
class Program
{
// ready the validation DLL for use
[DllImport("DocVal.dll")]
public static extern String DllMain(string path);


static void Main(string[] args)
{
try
{
// lannch our C++ DLL with the fully qualified path to the
document
String response =
DllMain(@"\\share\SharedDocs\test\Sometext.txt");
Console.WriteLine("Validation response is: " + response);
}
catch (Exception exec)
{
Console.WriteLine("There was an internal error with the DLL
:-( Details: " + exec.Message);
}
}
-----------------------
in C++: ( I choose to make a new dll project in the editor)
.....normal header stuff ....

#pragma data_seg("SharedBlockOMemory")
// shared stuff here... (if needed)
#pragma data_seg()

// these are the functions we can call outside of our DLL if need be
extern LPCSTR _declspec(dllexport) DllMain(char * pathToFile);


LPCSTR _declspec(dllexport) DllMain(char * pathToFile)
{
char inputFile[255]; // our input .txt file
bool verboseMode = false; // to keep track if we want everything
logged verbosely
bool ignoreFileExtension = false; // to see if we are to ignore the file
extension



sprintf(inputFile, "%s\0", pathToFile); // and store it into a char[]

inputFileCString = pathToFile; // assign this to a CString for use later



// ... your work here...

return /*(something in LPCSTR format)*/ "Hello!";


} // end DllMain ()

Note that you may have trouble using CStrings in your C++ code... I had to
take almost all of them out when I converted some of our stuff to dlls...
expecially CString return types from calling any functions within your C
code. I used char *'s and that seemed to work well.

Hope this gets you started...

Rob K
 
G

Guest

Thanks alot Rob n Jianwei. Its a com dll so I will try today as Jianwei
suggestion and tell you guys if it works.

Aravind.
Jianwei Sun said:
If it's a COM dll, then you are looking into Interop.

You need to add a reference to that COM dll.

At the top of the using, include the com interface like this:


using System.Runtime.InteropServices;
using *** YourCOM ***

Then, all the COM interface should be visible to you.

The code is like this :

m_oComObject = new ComObjectClass();


After finishing, do this:
Marshal.ReleaseComObject(m_m_oComObject);


HTH

Jianwei
Thank you for your reply. But the problem is I am using third party plugin. I
must use functions of a COM-interface that the plugin provides me. I think
now my problem is more clear to you. Do you have any solutions??


RobKinney1 said:
I am afraid I am not an expert in this, but here is how I got it to work in
one of our little applications....

C#:
class Program
{
// ready the validation DLL for use
[DllImport("DocVal.dll")]
public static extern String DllMain(string path);


static void Main(string[] args)
{
try
{
// lannch our C++ DLL with the fully qualified path to the
document
String response =
DllMain(@"\\share\SharedDocs\test\Sometext.txt");
Console.WriteLine("Validation response is: " + response);
}
catch (Exception exec)
{
Console.WriteLine("There was an internal error with the DLL
:-( Details: " + exec.Message);
}
}
-----------------------
in C++: ( I choose to make a new dll project in the editor)
.....normal header stuff ....

#pragma data_seg("SharedBlockOMemory")
// shared stuff here... (if needed)
#pragma data_seg()

// these are the functions we can call outside of our DLL if need be
extern LPCSTR _declspec(dllexport) DllMain(char * pathToFile);


LPCSTR _declspec(dllexport) DllMain(char * pathToFile)
{
char inputFile[255]; // our input .txt file
bool verboseMode = false; // to keep track if we want everything
logged verbosely
bool ignoreFileExtension = false; // to see if we are to ignore the file
extension



sprintf(inputFile, "%s\0", pathToFile); // and store it into a char[]

inputFileCString = pathToFile; // assign this to a CString for use later



// ... your work here...

return /*(something in LPCSTR format)*/ "Hello!";


} // end DllMain ()

Note that you may have trouble using CStrings in your C++ code... I had to
take almost all of them out when I converted some of our stuff to dlls...
expecially CString return types from calling any functions within your C
code. I used char *'s and that seemed to work well.

Hope this gets you started...

Rob K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top