Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Necuima

Passing A String From C++ To A C Dll

Recommended Posts

Hi,

 

I hope that this is not 'too simple' for anyone replying, but I'm a C novice so your advice will be most appreciated.

 

The background is that I'm trying to learn about DLLs and using them in a Windows 7 environment.

 

I have created a simple 'HelloWorld-ish' DLL in C which compiles OK.  It is meant to receive a string passed to it from a calling C++ program and display that string in a messagebox.

 

I am aware that strings are handled differently in C and C++.  I suspect that my 'caller' program is passing the string OK but that my C DLL is not handling it OK.  The DLL displays just the first character of the passed string.

 

Here's the code from the DLL - first the dll.h:

#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

DLLIMPORT void HelloWorld (char *str_in);

#endif /* _DLL_H_ */

And the HelloWorld.c code:

#include "E:/documents/C_PlusPlus/HelloWorld_C/dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
DLLIMPORT void HelloWorld (char *str_in)
{
    MessageBox (0, str_in, "Try 2", MB_ICONINFORMATION);
}

BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;
      case DLL_PROCESS_DETACH:
        break;
      case DLL_THREAD_ATTACH:
        break;
      case DLL_THREAD_DETACH:
        break;
    }
    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

My environment in this context is Windows 7 64 bit and Dev-C++

 

I have scoured the Internet and forums but I can't seem to find what I'm looking for.

 

Any advice will be most appreciated and thank you in anticipation.

 

Cheers

Share this post


Link to post
Share on other sites

Hi, after much Googling and trial and error I managed to create the dll in C++ (rather than my first attempt above in C). The C++ dll works!

 

Thanks to anyone who had a look at this.

 

Cheers from Oz.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...