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

My way of access C++ MFC code from C# Web App or Win form using a DLL

Hi, I thought I'd post my notes on how I went about accessing MFC C++
code in my web app after spending a day or more gathering information
on how to do it.
Please, let me know if you have a better way (especially with passing
strings into the DLL and getting strings back out), but I've found this
way is pretty simple!

Create a C++ MFC DLL project
1. expose methods to call like this in the main header, e.g.:
extern "C" __declspec(dllexport) int SomeFunction();
extern "C" __declspec(dllexport) char HelloWorld(char inputStr[1024]);
2. in the main cpp file, create the methods like this:
extern "C" __declspec(dllexport) int SomeFunction()
{
return 6;
}

extern "C" __declspec(dllexport) char HelloWorld(char inputStr[1024])
{
return inputStr[0];
}

3. compile up, and put into windows\system32 if you want it to be
accessible from ASP.Net web apps, OR if you use the LoadLibrary
technique (discussed below) you can place it where you want.
4. Make any folders (like ones that contain log file that need to be
written to by the MFC DLL) writable by the <computerName>\ASPNET user.

Create a c# Web App
1. Add the following "using": using System.Runtime.InteropServices;
2. Access the Dll using the DllImport command inside your class
definition, e.g.:
[DllImport("mfcdll.dll")]
public static extern char HelloWorld(string t);
[DllImport("mfcdll.dll")]
public static extern int SomeFunction();
3. Invoke methods on the dll as you would any method, e.g. in your
Page_Load:
int result = SomeFunction();
char result2 = HelloWorld("Tony");

If you need to access the DLL from a place other than windows\system32
1. Use Load Library to specify the dll path, by putting in the
following DllImport inside your class definition:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);
2. and before you call invoke DLL methods, invoke the LoadLibrary
method like this:
3. LoadLibrary(@"c:\MyPath\mfcdll.dll");

I hope this saves someone some time!
Cheers,
Tony.

Jun 13 '06 #1
0 1358

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mike Dwyer | last post by:
I need a VB 6 app to automate a microsoft access MDB application. Specifically, it needs to open a form in Access and read information keyed into that form. I know this is "old school" but I'm...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
20
by: Olav.NET | last post by:
I am a .NET/C++ developer who is supposed to do some work with Access. I do not know much about it except for the DB part. Questions: *1* I am looking for INTENSIVE books to get quickly up to...
55
by: AnandaSim | last post by:
I just had a google through this NG but have not seen mention of Erik Rucker's blog entry and the new Jet: http://blogs.msdn.com/access/archive/2005/10/05/477549.aspx mentioned by Mike...
29
by: Patrick | last post by:
I have the following code, which regardless which works fine and logs to the EventViewer regardless of whether <processModel/> section of machine.config is set to username="SYSTEM" or "machine" ...
34
by: Mathieu Trentesaux | last post by:
Hello I downloaded Office 2007 for this reason : It seems, once again, that it is impossible to save any modification done in a VBA library, from the main project in Access. The save button...
16
by: John | last post by:
I am looking for VBA code that will work with Access 2003 to enable dragging and dropping a file/folder name from Windows XP Explorer into an Access form's text box. This is a common functionality...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
6
by: Wesley Peace | last post by:
I hate to cross post, but I've gotten no answer yet on a problem I'm having with visual studio 2008. I've created a series of forms with controls to access a Access database tables. The...
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.