INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close