• Welcome to Valhalla Legends Archive.
 

Exporting C Functions in a DLL

Started by FrostWraith, October 18, 2006, 03:55 PM

Previous topic - Next topic

MyndFyre

Quote from: FrostWraith on October 21, 2006, 12:11 AM
WOW! New strange problem, if I call this function 2 times i crash! Wtf? Should I set everything to null at the end of the function?
It's not really that strange!  Take a look at your code!  You're declaring s1, but you're never allocating memory.  You're overwriting arbitrary memory.  Very, very scary!  You should definitely allocate memory that you're going to memcpy or strcpy to!
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

K

That is wrong on so many levels.  I would try something like this:


int __stdcall get_version (char* out)
{
    const char* version = "1.0.0";
    strcpy( out, version);
    return strlen(version);
}



Public Declare Function [Whatever] (ByVal out As String) As Long

Dim buffer As String
Dim len as Long

buffer = Space(255);
len = Whatever(buffer);
buffer = Left(buffer, len);

FrostWraith

#17
I'm starting to understand this space thing. So basically what you want to do before you call the get_version function is free up some space that can be over written by the function?  If so, should this standard be used when calling, say, a local VB function?

Also: If the length of the string is static, should I just lower the space to (in this case) 5?

Kp

Five is not sufficient.  You're forgetting the terminating null character.

You don't need to preallocate space for calls between functions in VB (as far as I know), but generally you will need to preallocate when calling between different languages.  If you were staying purely in C, you could let the callee allocate it and burden the caller with freeing it.  Strictly speaking, you can do that with VB->C as well, but it's more troublesome.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!