473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Calling MFC DLL from C#

alexgm23
Hi,

I'm trying to call a MFC DLL function from C# code, but I just don't know how to do it.

I've got the DLL's function names using "Dependency Walker" but they look like this:

?AddApl@CDocsHeadings@@QAEHPAVCDocsApl@@@Z
?AddItem@CDocsHeadings@@QAEHPAVCDocsItems@@H@Z
?AddNew@CDocsHeadings@@UAEXXZ
?AddRef@CDocsHeadings@@QAEXDVCString@@@Z
.....

sgsql.dll.jpg

I've got the C++ header files used to make the DLL, so I know how many parameters and what parameter types the DLL's functions expect.

Any idea on how can I accomplish this?


Alejandro Gutierrez
Mar 11 '09 #1
4 10367
Those look like debug strings. Build -> Clean Solution. Ensure the Configuration is set to Release. Then Build -> Build Solution

Craig
Mar 17 '09 #2
Thanks Craig.

I already found out what those strings mean and how to deal with them, but I still need (too much) help on import a function from the DLL that has a return value of type 'CString &'.

Can you help me on this?

Thanks in advance.
Mar 17 '09 #3
Well this can be attacked in many different ways, but heres how I would do it;

Have the function return 'const char*' instead of 'CString'

CString theString( "This is a test" );
char* mychar = new char[theString.GetLength()+1];
_tcscpy(mychar, theString);
return mychar;

then in C# use DLLImport to import the function which it will now return a normal C# string that is recognised and terminated by a null value.

If you still have any problems, let me know

Craig
Mar 17 '09 #4
Thanks again!

After doing a research I got the same answer, and I think that's the best. But I haven't been explicit enough. The fact is that I can't add such wrapper function returning something more suitable like char* because I don't have the DLL code.

There's a way to instantiate a C++ class from a DLL. You can call a function that returns a pointer such as a class constructor (returns this). Then in C# you have to declare a struct that properly holds the this pointer:

Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  2. public unsafe struct __CString
  3. {
  4.     public IntPtr* _vtable;
  5.     public int m_Value;
  6. }
  7.  
Then you can make a C# wrapper class for the C++ class CString:

Expand|Select|Wrap|Line Numbers
  1. public unsafe class CString : IDisposable
  2. {
  3.     private __CString* _cpp;
  4.  
  5.     [DllImport("mfc42.dll", EntryPoint = "#535", CallingConvention = CallingConvention.ThisCall)]
  6.     private static extern int _CString_Constructor(__CString* ths, CString str);
  7.  
  8.     [DllImport("mfc42.dll", EntryPoint = "#537", CallingConvention = CallingConvention.ThisCall)]
  9.     private static extern int _CString_Constructor(__CString* ths, [MarshalAs(UnmanagedType.LPStr)] String str);
  10.  
  11.     [DllImport("mfc42.dll", EntryPoint = "#540", CallingConvention = CallingConvention.ThisCall)]
  12.     private static extern int _CString_Constructor(__CString* ths);
  13.  
  14.     [DllImport("mfc42.dll", EntryPoint = "#800", CallingConvention = CallingConvention.ThisCall)]
  15.     private static extern void _CString_Destructor(__CString* ths);
  16.  
  17.     [DllImport("mfc42.dll", EntryPoint = "#2915", CallingConvention = CallingConvention.ThisCall)]
  18.     [return: MarshalAs(UnmanagedType.LPStr)]
  19.     private static extern String _CString_GetBuffer(__CString* ths, int Length);
  20.  
  21.     public CString(CString str)
  22.     {
  23.         _cpp = (__CString*)Memory.Alloc(sizeof(__CString));
  24.         _CString_Constructor(_cpp, str);
  25.     }
  26.  
  27.     public CString(String str)
  28.     {
  29.         _cpp = (__CString*)Memory.Alloc(sizeof(__CString));
  30.         _CString_Constructor(_cpp, str);
  31.     }
  32.  
  33.     public CString()
  34.     {
  35.         _cpp = (__CString*)Memory.Alloc(sizeof(__CString));
  36.         _CString_Constructor(_cpp);
  37.     }
  38.  
  39.     public void Dispose()
  40.     {
  41.         _CString_Destructor(_cpp);
  42.         Memory.Free(_cpp);
  43.     }
  44.  
  45.     public String GetBuffer()
  46.     {
  47.         return _CString_GetBuffer(_cpp, 255);
  48.     }
  49. }
  50.  
Actually, I can instatiate a CString in this way without any problems:

Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         CString s = new CString("Hello, World!");
  6.         Console.WriteLine(s.GetBuffer());
  7.     }
  8. }
  9.  
But when I try to do this:

Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Sample s = new Sample(1000);
  6.         Console.WriteLine(s.AsString().GetBuffer());
  7.     }
  8. }
  9.  
  10. /*
  11.   CString code...
  12. */
  13.  
  14. /*
  15.   Sample Class Wrapper
  16. */
  17.  
  18. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  19. public unsafe struct __Sample
  20. {
  21.     public IntPtr* _vtable;
  22.     public int m_Value;
  23. }
  24.  
  25. public unsafe class Sample : IDisposable
  26. {
  27.     private __Sample* _cpp;
  28.  
  29.     [DllImport("OldMFC.dll", EntryPoint = "??0Sample@@QAE@H@Z", CallingConvention = CallingConvention.ThisCall)]
  30.     private static extern int _Sample_Constructor(__Sample* ths, int Value);
  31.  
  32.     [DllImport("OldMFC.dll", EntryPoint = "??1Sample@@UAE@XZ", CallingConvention = CallingConvention.ThisCall)]
  33.     private static extern void _Sample_Destructor(__Sample* ths);
  34.  
  35.     [DllImport("OldMFC.dll", EntryPoint = "?AsString@Sample@@QAEAAVCString@@XZ", CallingConvention = CallingConvention.ThisCall)]
  36.     private static extern CString _Sample_AsString(__Sample* ths);
  37.  
  38.     [DllImport("OldMFC.dll", EntryPoint = "?GetValue@Sample@@QAEHXZ", CallingConvention = CallingConvention.ThisCall)]
  39.     private static extern int _Sample_GetValue(__Sample* ths);
  40.  
  41.     public Sample(int Value)
  42.     {
  43.         _cpp = (__Sample*)Memory.Alloc(sizeof(__Sample));
  44.         _Sample_Constructor(_cpp, Value);
  45.     }
  46.  
  47.     public void Dispose()
  48.     {
  49.         _Sample_Destructor(_cpp);
  50.         Memory.Free(_cpp);
  51.     }
  52.  
  53.     public CString AsString()
  54.     {
  55.         return _Sample_AsString(_cpp);
  56.     }
  57. }
  58.  
  59. /*
  60.   Memory Class code...
  61. */
  62.  
I get this error:

Unspecied error
Exception from HRESULT: 0x80004005 (E_FAIL)
The DLL function declaration is:

CString & Sample::AsString();
Any idea about it?

Alex G.
Mar 17 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
7
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. ...
2
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.