Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling MS Visual C++ wrapper dll from C code application

Status
Not open for further replies.

anon12

Programmer
Sep 17, 2013
1
0
0
CA
Hello Everyone,
I am new to the programming world and doing a project for a friend. What is required of me to do is send an email from Outlook 2010 from a legacy C application (using Borland 5.02). I realize that I need a mixed mode (managed and unmanaged) wrapper to allow the C app to call C# dll. I have written a wrapper in MS Visual C++ and am able to send emails from this c++ wrapper(using a native app written in C++). I am using explicit linking, so that I can provide updates for the dlls without having to worry about keeping track of multiple files (lib files) to send for any patches/updates. Since my C++ to C# code is functioning properly I will only post the section that doesn't work (which is the C code to call the MS Visual C++ dll).
Example Code:
/******************C++ Code**************************************/
SendMail.h (C++ dll header for linking with Borland C app)
#ifndef SendMail_H
#define SendMail_H
#define DLLEXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C" {
#endif
DLLEXPORT int SendMail (char *To, char *From,char *Subject, char *Body);

#ifdef __cplusplus
}
#endif

#endif /* SendMail_H */

SendMail.cpp (C++ source code for sending mail)
#include "SendMail.h"
namespace MailWrapper //<<--Contains the C# dll
{

#ifdef __cplusplus
extern "C"
{
#endif
DLLExport int SendMail(char* To,char* From,char *Subject,char *Body)
{
Mail ^ mail = clsMail::clsMail_Instance->mailObj;
int ret=0;
EmailFields ^ mngdFields= mail->GetEmailFields(); //get fields from C# dll
String ^ strTo=gcnew String(To);
mngdFields->To=strTo;
...
...
String ^ strBody=gcnew String(Body);
mngdFields->Body=strBody;
/**************************End of C++ code**************************/

I am calling this C++ dll from C code(using Borland 5.02). I store the compiled SendMail.dll in my C application project folder. Since I am using explicit linking I only need the dlls. I have use the TDUMP, IMPLIB, IMPDEF and TLIB utilities to see if the SendMail.dll name decoration matches Borland's naming convention and it does ( I am not using the generated Lib files because I am using explicit linking). I also realize that the MS Visual C++ uses COFF and Borland uses OMF to generate the .lib file from dlls. I have also verified that there is no name mangling that usually takes place when going from MS Visual C++ to Borland .

/****************C Code *********************************/
#include <conio.h>
#include <ctype.h>
#include <io.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "windows.h"
typedef struc
{
char *To,
*From,
*Subject,
*Body;
}msgFields;
typedef int (*SendMail_FromSDG) (char*,char*,char*,char*); //function ptr

void main(void){
msgFields newMsg;
newMsg.To="test@myserver.com";
newMsg.From="from@myserver.com";
newMsg.Subject="Test Email";
newMsg.Body ="Test body";
SendMailFromC(newMsg);

}

int SendMailFromC(msgFields newMsg)
{

SendMail_FromSDG setupMailFields;
HINSTANCE hinstDLL;

hinstDLL=LoadLibrary("SendMail.dll");
if (hinstDLL!=NULL){
setupMailFields = (SendMail_FromSDG) GetProcAddress(hinstDLL,"SendMail");
sentMail=setupMailFields(newmsg->To,newmsg->From,newmsg->Subject,newmsg->Body); //Program crashes here!!!
FreeLibrary(hinstDLL);
}
}

/**************End of C Code****************************/
I do get the address for SendMail function in the DLL using the GetProcAddress(). It seems that the issue occurs when I try to send the mail. Can anyone offer some advice with regards to the crash or other feedback?
thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top