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
4 10367
Those look like debug strings. Build -> Clean Solution. Ensure the Configuration is set to Release. Then Build -> Build Solution
Craig
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.
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
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: -
[StructLayout(LayoutKind.Sequential, Pack = 4)]
-
public unsafe struct __CString
-
{
-
public IntPtr* _vtable;
-
public int m_Value;
-
}
-
Then you can make a C# wrapper class for the C++ class CString: -
public unsafe class CString : IDisposable
-
{
-
private __CString* _cpp;
-
-
[DllImport("mfc42.dll", EntryPoint = "#535", CallingConvention = CallingConvention.ThisCall)]
-
private static extern int _CString_Constructor(__CString* ths, CString str);
-
-
[DllImport("mfc42.dll", EntryPoint = "#537", CallingConvention = CallingConvention.ThisCall)]
-
private static extern int _CString_Constructor(__CString* ths, [MarshalAs(UnmanagedType.LPStr)] String str);
-
-
[DllImport("mfc42.dll", EntryPoint = "#540", CallingConvention = CallingConvention.ThisCall)]
-
private static extern int _CString_Constructor(__CString* ths);
-
-
[DllImport("mfc42.dll", EntryPoint = "#800", CallingConvention = CallingConvention.ThisCall)]
-
private static extern void _CString_Destructor(__CString* ths);
-
-
[DllImport("mfc42.dll", EntryPoint = "#2915", CallingConvention = CallingConvention.ThisCall)]
-
[return: MarshalAs(UnmanagedType.LPStr)]
-
private static extern String _CString_GetBuffer(__CString* ths, int Length);
-
-
public CString(CString str)
-
{
-
_cpp = (__CString*)Memory.Alloc(sizeof(__CString));
-
_CString_Constructor(_cpp, str);
-
}
-
-
public CString(String str)
-
{
-
_cpp = (__CString*)Memory.Alloc(sizeof(__CString));
-
_CString_Constructor(_cpp, str);
-
}
-
-
public CString()
-
{
-
_cpp = (__CString*)Memory.Alloc(sizeof(__CString));
-
_CString_Constructor(_cpp);
-
}
-
-
public void Dispose()
-
{
-
_CString_Destructor(_cpp);
-
Memory.Free(_cpp);
-
}
-
-
public String GetBuffer()
-
{
-
return _CString_GetBuffer(_cpp, 255);
-
}
-
}
-
Actually, I can instatiate a CString in this way without any problems: -
class Program
-
{
-
static void Main(string[] args)
-
{
-
CString s = new CString("Hello, World!");
-
Console.WriteLine(s.GetBuffer());
-
}
-
}
-
But when I try to do this: -
class Program
-
{
-
static void Main(string[] args)
-
{
-
Sample s = new Sample(1000);
-
Console.WriteLine(s.AsString().GetBuffer());
-
}
-
}
-
-
/*
-
CString code...
-
*/
-
-
/*
-
Sample Class Wrapper
-
*/
-
-
[StructLayout(LayoutKind.Sequential, Pack = 4)]
-
public unsafe struct __Sample
-
{
-
public IntPtr* _vtable;
-
public int m_Value;
-
}
-
-
public unsafe class Sample : IDisposable
-
{
-
private __Sample* _cpp;
-
-
[DllImport("OldMFC.dll", EntryPoint = "??0Sample@@QAE@H@Z", CallingConvention = CallingConvention.ThisCall)]
-
private static extern int _Sample_Constructor(__Sample* ths, int Value);
-
-
[DllImport("OldMFC.dll", EntryPoint = "??1Sample@@UAE@XZ", CallingConvention = CallingConvention.ThisCall)]
-
private static extern void _Sample_Destructor(__Sample* ths);
-
-
[DllImport("OldMFC.dll", EntryPoint = "?AsString@Sample@@QAEAAVCString@@XZ", CallingConvention = CallingConvention.ThisCall)]
-
private static extern CString _Sample_AsString(__Sample* ths);
-
-
[DllImport("OldMFC.dll", EntryPoint = "?GetValue@Sample@@QAEHXZ", CallingConvention = CallingConvention.ThisCall)]
-
private static extern int _Sample_GetValue(__Sample* ths);
-
-
public Sample(int Value)
-
{
-
_cpp = (__Sample*)Memory.Alloc(sizeof(__Sample));
-
_Sample_Constructor(_cpp, Value);
-
}
-
-
public void Dispose()
-
{
-
_Sample_Destructor(_cpp);
-
Memory.Free(_cpp);
-
}
-
-
public CString AsString()
-
{
-
return _Sample_AsString(_cpp);
-
}
-
}
-
-
/*
-
Memory Class code...
-
*/
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |