Create C# dll that acts like a C++ dll
-
There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.
-
There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.
AFAIK that's not possible. C# is managed code, while C++ is unmanaged code. Data and object marshaling in both is very different -not to mention the structure of the dll file, and the method of function exporting-.
Regards:rose:
-
AFAIK that's not possible. C# is managed code, while C++ is unmanaged code. Data and object marshaling in both is very different -not to mention the structure of the dll file, and the method of function exporting-.
Regards:rose:
While I don't know much about C# I am aware that MS went to some effort to make managed code and native code interoperate. I’d be willing to bet it is possible.
Steve
-
While I don't know much about C# I am aware that MS went to some effort to make managed code and native code interoperate. I’d be willing to bet it is possible.
Steve
Stephen Hewitt wrote:
I’d be willing to bet it is possible.
If you do, then why didn't you tell us how to do it! Yes, it's possible somehow, but not in his scenario. You may mean the COM wrapper of DotNet that enables you to make your objects COM visible. He wants to make a C# dll for a program that references a C++ dll without COM, and my bet is that the program doesn't expect a managed code either. Also note that the function doesn't belong to a class which is not allowed in C#.
Regards:rose:
-
Stephen Hewitt wrote:
I’d be willing to bet it is possible.
If you do, then why didn't you tell us how to do it! Yes, it's possible somehow, but not in his scenario. You may mean the COM wrapper of DotNet that enables you to make your objects COM visible. He wants to make a C# dll for a program that references a C++ dll without COM, and my bet is that the program doesn't expect a managed code either. Also note that the function doesn't belong to a class which is not allowed in C#.
Regards:rose:
Here's what I said:
Stephen Hewitt wrote:
While I don't know much about C# I am aware that MS went to some effort to make managed code and native code interoperate. I’d be willing to bet it is possible.
Nader Elshehabi wrote:
If you do, then why didn't you tell us how to do it!
Isn't the answer to this question obvious? "I don't know much about C#". I don't know how, it's just my opinion that it’s possible. What’s more, I’d wager it’s not that complex.
Steve
-
There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.
As already explained by others, C# can only produce managed code, while your sample is an unmanaged DLL. There are several ways to implement managed-unmanaged interoperability, but there is no simple solution for what you want to do. One idea that come to my mind requires the creation of two DLLs: One unmanaged C++ DLL that exports your required method and transforms it into a COM method call. And a COM server DLL that provides the method consumed by the first DLL. The second DLL can be written in C# as managed DLLs can be designed to be callable by COM clients (CCW).
Regards, Tim
-
There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.
-
As already explained by others, C# can only produce managed code, while your sample is an unmanaged DLL. There are several ways to implement managed-unmanaged interoperability, but there is no simple solution for what you want to do. One idea that come to my mind requires the creation of two DLLs: One unmanaged C++ DLL that exports your required method and transforms it into a COM method call. And a COM server DLL that provides the method consumed by the first DLL. The second DLL can be written in C# as managed DLLs can be designed to be callable by COM clients (CCW).
Regards, Tim
Well, I actually expected it was the case that I could not fully duplicate the external behavior of a C++ dll, but I wanted to make sure. The C++ wrapper dll occurred to me and would be possible since we have C++ developers, but it is not a good solution. Ultimately the problem was solve in another manner. We are communicating through he standard output stream and it works great. Thanks for everyones comments.