Sign in to follow this  

Exporting string values from a DLL...

This topic is 3585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The safest way is to have a function that takes a char* (and for added safety, the size of the buffer pointed to), and then have the function in the DLL fill that buffer with data using sprintf() or some similar function.

Another option would be to sprintf() (or similar) to a static buffer and return that.

Returning memory allocated from new or malloc, or returning a std::string isn't advisable, since the DLL and EXE may have different heaps.

EDIT: Actually, there's another way I thought of, which is even more horrible:

struct FixedString
{
char buff[128];
};

FixedString GetDouble()
{
FixedString ret;
snprintf(ret.buff, sizeof(ret.buff), "%f", someDouble);
return ret;
}

// Usage:
printf("DLL returns %s\n", GetDouble().buff);


That returns an array of chars in a struct (There might be a way to do it without the struct, I'm not sure offhand) by value. But like I said, it's horrible.

Share this post


Link to post
Share on other sites
If you want a DLL that just stores string values, then consider making a DLL that just contains string table resources. You can then use LoadLibrary() and LoadString() to get the strings without needing to manage character memory. However, I'm not familiar with DevCpp's resource editing capabilities; you'll need to consult your compiler's documentation to find out how to do that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve

Returning memory allocated from new or malloc, or returning a std::string isn't advisable, since the DLL and EXE may have different heaps.



AFAIK when the DLL versions of the VC++ CRT load the DLL, they both have the same heap manager. But of course your suggestion is probably the way to go, considering I have no idea how mingw handles heap management for DLL's.

Speaking of mingw, may I humbly suggest that you ditch DevC++ in favor of Visual C++? [smile]

Share this post


Link to post
Share on other sites

This topic is 3585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this