TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Davide
NA
4
0
using c++ MFC dll in c# code - SEHExceptio
Nov 28 2008 5:42 AM
Hi,
I am a computer science student and I am developing a web-application in C# for a company as my master thesis. The web-application I am developing is an additional tool for an existent desktop application written in C++.
One of the requirements I have to accomplish is to keep the same layout interface in a web page as a window in the desktop application. This window contains a grid where there are some objects that the customers can drag and drop. The desktop application loads this grid at the window start-up and saves the grid every time it is modified (and the window shuts down). The grid is saved in a binary format by using serialization.
So I need to create a MFC dll as middle layer between my web application and the classes of desktop application that saves and load the grid. The grid’s objects are saved in a CMapToStringOb object, and this object is inside another class that is the one saved on the binary file. To better understand:
class CObjectLayout : public CObject {
DECLARE_SERIAL(CObjectLayout)
//.. constructor decostructor
public:
virtual void Serialize(CArchive& ar);
private:
CMapStringToOb* objectView;
// .. other data
}
An instance of this class is saved in the binary file. In fact, in the window class contains the following Serialize method
void CWindow::Serialize(CArchive& ar) {
if (ar.IsStoring()) {
//objectLayout is defined as CObjectLayout*
ar.WriteObject(objectLayout);
}
else {
if( m objectLayout != NULL )
delete m objectLayout;
objectLayout = (CObjectLayout *) ar.ReadObject(RUNTIME_CLASS(CObjectLayout));
}
}
I created my MFC Dll called CMiddleLayer as
class CMiddleLayer {
public:
CMiddleLayer();
~CMiddleLayer();
private:
CObjectLayout* objectLayout;
public:
virtual void Serialize(CArchive &ar);
LPCTSTR returnXPosition(LPCTSTR objectName);
// .. and other similar methods
};
The Serialize method is exactly the same as the one in CWindow class and it is called inside the method returnXPosition (it is not a good thing load all everytime, but it is just to try if it works).
I also added the following functions to export the class methods:
extern "C" __declspec(dllexport) CMiddleLayer* MiddleLayer_Create(){return new CMiddleLayer();}
extern "C" __declspec(dllexport) LPCTSTR MiddleLayer_Return(CMiddleLayer* MiddleLayer, LPCTSTR objectName){return MiddleLayer-> returnXPosition (objectName);}
extern "C" __declspec(dllexport) void MiddleLayer_Delete(CMiddleLayer* MiddleLayer){delete MiddleLayer;}
In c# code I imported the dll in the following way:
[DllImport(@"myDllMFC.dll")]
private static extern IntPtr MiddleLayer_Create();
[DllImport(@"myDllMFC.dll")]
private static extern string MiddleLayer_Return(IntPtr value, string objectName);
[DllImport(@"myDllMFC.dll")]
private static extern void MiddleLayer_Delete(IntPtr value);
I created a label wherein loads the XPosition:
IntPtr pointer = MiddleLayer_Create();
Label.Text = MiddleLayer_Return(pointer, “objectName”);
When I try to run the web application, it gives back a SEHException at the Label.Text=.. line.
I found out that the MFC dll crashes at the line wherein the CObjectLayout object is loaded from the file (objectLayout=(CObjectLayout *)..).
I tried everything I found around the web but I don’t understand how to solve it. If it can help, I tried to take out just a byte from the file and it works.
Can anyone help me?
Do you think there is a way to solve it by carrying on using this way to save and load the data?
At least tell me if there is no way to solve it and I will try to save the object data in a different way, even if it will take more time.
Thans a lot
Davide
P.S. I am italian.. so sorry for my english..
Reply
Answers (
2
)
Need help with marshaling in C#...
C# Compiler Detecting Overflow?