473,395 Members | 2,010 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,395 software developers and data experts.

Using functions in a VC++ 6.0 MFC DLL

alexgm23
I have a VC++ 6.0 MFC DLL I developed long time ago and I need to use it on a C# project. I can't re-develop it again in C# because it's too BIG (160 different classes and 540 functions) and I can't spend time on that.

I know how to instatiate the C++ classes that are in that DLL, here is a sample code:

Expand|Select|Wrap|Line Numbers
  1. //Sample.h
  2.  
  3. public class __declspec(dllexport) Sample
  4. {
  5. public: 
  6.     Sample(int Value);
  7.     virtual ~Sample();
  8.     int GetValue();
  9.     CString & AsString();
  10.  
  11. private: 
  12.     int m_iValue;
  13.     CString m_sValue;
  14. };
  15.  
  16. //---------------------------------------------------------------
  17.  
  18. //Sample.cpp
  19.  
  20. Sample::Sample(int Value)
  21. {
  22.     m_iValue = Value;
  23.     m_sValue.Format("AsString: %i", m_iValue);
  24. }
  25.  
  26. Sample::~Sample()
  27. {
  28. }
  29.  
  30. int Sample::GetValue()
  31. {
  32.     return m_iValue;
  33. }
  34.  
  35. CString & Sample::AsString()
  36. {
  37.     return m_sValue;
  38. }
  39.  
  40. //-----------------------------------------------------------------
  41.  
  42. //Form1.cs
  43.  
  44. public partial class Form1 : Form
  45. {
  46.     public Form1()
  47.     {
  48.         InitializeComponent();
  49.         LoadSample();
  50.     }
  51.  
  52.     public void LoadOldMFC()
  53.     {
  54.         Sample s = new Sample(450);
  55.         textBox1.AppendText(s.GetValue().ToString() + "\r\n");
  56.         textBox1.AppendText(s.AsString() + "\r\n");
  57.     }
  58. }
  59.  
  60. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  61. public unsafe struct __Sample
  62. {
  63.     public IntPtr* _vtable;
  64.     public int m_Value;
  65. }
  66.  
  67. public unsafe class Sample : IDisposable
  68. {
  69.     private __Sample* _cpp;
  70.  
  71.     [DllImport("OldMFC.dll", EntryPoint = "??0Sample@OldMFC@@QAE@H@Z", CallingConvention = CallingConvention.ThisCall)]
  72.     private static extern int _Sample_Constructor(__Sample* ths, int Value);
  73.  
  74.     [DllImport("OldMFC.dll", EntryPoint = "??1Sample@OldMFC@@UAE@XZ", CallingConvention = CallingConvention.ThisCall)]
  75.     private static extern void _Sample_Destructor(__Sample* ths);
  76.  
  77.     [DllImport("OldMFC.dll", EntryPoint = "?GetValue@Sample@OldMFC@@QAEHXZ", CallingConvention = CallingConvention.ThisCall)]
  78.     private static extern int _Sample_GetValue(__Sample* ths);
  79.  
  80.     [DllImport("OldMFC.dll", EntryPoint = "?AsString@Sample@OldMFC@@QAEAAV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@XZ", CallingConvention = CallingConvention.ThisCall)]
  81.     private static extern string _Sample_AsString(__Sample* ths);
  82.  
  83.     public Sample(int Value)
  84.     {
  85.         _cpp = (__Sample*)Memory.Alloc(sizeof__Sample));
  86.         _Sample_Constructor(_cpp, Value);
  87.     }
  88.  
  89.     public void Dispose()
  90.     {
  91.         _Sample_Destructor(_cpp);
  92.         Memory.Free(_cpp);
  93.     }
  94.  
  95.     public int GetValue()
  96.     {
  97.         return _Sample_GetValue(_cpp);
  98.     }
  99.  
  100.     public string AsString()
  101.     {
  102.         return _Sample_AsString(_cpp);
  103.     }
  104. }
  105.  

I have no problem on instantiating the class or calling function GetValue, but when I call function AsString, I only get garbage.

Someone knows how can I have this working?
Is there something I'm missing?
Is there some way to "cast" MFC class CString to System.String?
Alex G.
Mar 13 '09 #1
0 1653

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

Similar topics

2
by: Peter Nolan | last post by:
Hi All, I am testing some VC++ code from win2000 on a Solaris machine. I've found a couple of functions that are in VC++ but not the sun compiler. The two so far are: itoa - Integer to asci...
8
by: Scott Allen | last post by:
Hello, I'm new to C++ development and I'm trying out figure out the cause of an 'unresolved external symbol' error that I'm receiving when compiling. Here is some history on what I'm doing: I...
34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
5
by: Bryan Parkoff | last post by:
Please provide me the information when it is not in the correct newsgroups. I have been using C and C++ language for a long time when it is time for me to learn Win32 API functions that they can...
17
by: Ralph | last post by:
Hi all, Recently I have tried to create a static lib using MS VC++. The following are some of the excerpt of my codes: ********* MyFirstStaticLib.c ******************* #include...
1
by: DocR | last post by:
I have some VC++ tested code that I want to use from C#.NET. I know I have methods to port the code from VC++ code to .NET but my code is tested and I do not want to port that to C#.NET. Then I know...
1
by: Anna | last post by:
Hi, I'm using VS.NET 2003 on Win2000 (SP3). I am trying to import a dll developed in VC++6.0 into my VC.net project. I keep getting linker errors complaining about GetThisClass and...
1
by: sandy111 | last post by:
here i hav a vc++ dll with some functions defined so that i can import from other applications. how can i import the same functions of vc++ in my c# applications so that i can use those...
3
by: valoh | last post by:
Hi, is this legal c++ code? template <typename BaseTstruct A { BaseT& this_() { return *static_cast<BaseT*>(this); } template <typename Tvoid Foo() { this_().Bar<T>(); } template...
7
by: Ryanivanka | last post by:
hi, I am using a delphi DLL in vc++,static linked with ".h"and "lib". the export functions in DLL are "__stdcall",but the function doesn't work well,it often returns some weird values. when I add...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.