• Welcome to Valhalla Legends Archive.
 

Exporting C Functions in a DLL

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

Previous topic - Next topic

FrostWraith

OK, I am stumped!

Here is what I have gotten to work

test.c
int _stdcall SendNumber() {
    return 1;
}


test.def
EXPORTS
SendNumber


Then as soon as a add a parameter for the SendNumber function like so:
int _stdcall SendNumber(int inputnum) {
    return inputnum;
}


It definantly does not return the correct number.
I am using GCC command line compiler.

Kp

How do you know it is returning the wrong number?  How are you calling it?  Are you trusting the linker to fix up the reference or are you locating it dynamically via GetProcAddress?
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

FrostWraith

You lost me there. I am calling it via VB (unfortunatly my most fluent language). It is returning strange numbers that change each time it is recompiled. And I guess I must be trusting the linker, whatever that is.

UserLoser

What does your declaration function look like for this?

FrostWraith

Private Declare Function SendNumber Lib "E:\test.dll" (inputnum As Integer) As Integer

UserLoser

Not sure if this'll change anything, but try:


Private Declare Function SendNumbe Lib "E:\Test.dll" (ByVal InputNum As Long) As Long

FrostWraith

#6
Strange enough, it did. Wtf? Works good for now. Here's the next problem: Strings.

Note: All that was needed was ByVal.

Also, when I compile I get this (I changed the function btw):
QuoteWarning: resolving _get_verbyte by linking to _get_verbyte@4
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups

It compiled and worked all nicely but, I didn't like seeing errors. So i went into the def file and predefines the linking to @4 like so:
EXPORTS
    get_verbyte@4


Now it cant find dll entry point.

UserLoser

Quote from: FrostWraith on October 18, 2006, 11:36 PM
Strange enough, it did. Wtf? Works good for now. Here's the next problem: Strings.

Note: All that was needed was ByVal.

Also, when I compile I get this (I changed the function btw):
QuoteWarning: resolving _get_verbyte by linking to _get_verbyte@4
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups

It compiled and worked all nicely but, I didn't like seeing errors. So i went into the def file and predefines the linking to @4 like so:
EXPORTS
    get_verbyte@4


Now it cant find dll entry point.

Public Declare Function GetVersionByte Alias "#4" Lib "Whatever.dll (...) As ...

FrostWraith

If I add that, it doesn't work. Does anyone here know how to properly declare an exported fuction in a .def file. I just don't like seeing warnings.

MyndFyre

Decorate your exported function signature as


extern "C"
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.

l)ragon

#10
Quote from: FrostWraith on October 19, 2006, 04:09 PM
If I add that, it doesn't work. Does anyone here know how to properly declare an exported fuction in a .def file. I just don't like seeing warnings.
eg:
LIBRARY GUI

EXPORTS
CreateREB
DestroyREB
AppendTextEX
TimeStamper
InsertItem
RemoveItem
GetListViewItemText
GetListViewSelectedItem
GetItemIndex
ModifyItem
ClearListView
SetEditBoxText
GetEditBoxText
AppendEditBoxText
GetIconCode
GetPingCode
MakeTransparent
AddChat
ClearRichEdit
InitImageList
SetRgbBG
CreateLV

__stdcall
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

FrostWraith

OK. I cannot figure out how to pass strings through my C function and return the exact same string. Is "char*" the same data type as "As String" in VB? If it is, do i need to use ByRef to retrieve it in VB? Help me.

FrostWraith

#12
So how would I go about sending the parameter with the same data type that is char*?

O have tried:
unsigned char * _stdcall get_version ()
{
    return "1.0.0";
}

UserLoser

Public Declare Function get_version lib "whatever.dll" () As Long

Lookup lstrcpy and lstrlen for this operation

FrostWraith

#14
Thank You!

Here was the solution for anyone who ever happens to need help.
char* __stdcall get_version ()
{
    char* s1;
    const char* s2 = "1.0.0";
    char* ptr;

    ptr = strcpy( s1, s2 );
    return ptr;
}


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?