Jump to content
Tuts 4 You

Static lib C return value problems


LCF-AT

Recommended Posts

Hi guys,

so I have just a little question about static libs (dont find any older topics to post my question).

So I wrote some dll code and created some export functions with MASM and now I have the normal buld lib of the dll like other libs like kernel32 etc (you need to have kernel dll).Now if I build a new project and use my lib with some functions then it will also need the dll where I used the lib from and this I dont wanna have and would like to build a static lib from my lib so that I dont need the dll itself later in my project.All clear so far right.Now I used a tool called dll to lib to build a lib from my dll (which is in MASM compiled dll) to get a static lib I can use in my projects later.So I did use the tool and got the static lib and use it but there is one bad problem.If I want to use my functions then I have to declare these as C functions and then I get the file also compiled BUT now I have some return value problems after my functions.So with C it does sub the return values after a function call BUT its not needed so they get already sub inside my functions.So how can I use my functions without PROTO C ..to prevent the sub after calling my functions?

So of course I could use it also as it is but in this case I always have add value xy after calling any of my functions but this looks stupid etc you know.
 

call Function A
sub esp,14
add esp,14

Or is there any other way how to build a static lib with MASM or any tools there etc?Maybe you know some more and can help a little.

Thank you

Link to comment

declare it as stdclall if its compiled as stdcall, if its cdecl then the sub esp, 14 etc is normal compiler optimisation, the return value from the func is always meant to be in eax (or in params passed it its a struct or whatnot)

 

Link to comment

What and how you mean it?The file is compiled with .model flat,stdcall.

If I dont use C after PROTO then it will not compiled "unresolved external symbol xy@16"

greetz

Link to comment

in masm

 

blahblah proc uses ebx ecx edx esi edi, param1:dword

mov eax, [param1]

ret

blahblah endp

 

then in the h file for the code just declare it like

DWORD __stdcall blahblah(DWORD parampassed);

 

is how i usually do it

if you still cant get it working, i'll make you a small skeleton code, that works at my end with masm and cl.exe for the c stuff atm though its bedtime for me, work in 5 hours ..

Edited by evlncrn8
  • Like 1
Link to comment

Hi again,

hmmm.So I use WinASM and there its not working.Below how it works with the return value problem if I use the created static lib in new project....

API_1  proto  C  :DWORD,:DWORD,:DWORD,:DWORD

invoke API_1,eax,ecx,esi,edi

....if I remove the C after proto then it dosent work.So where I have to set the stdcall there?

Oh and one more info.In the WinASM properties of my project I did yesterday enable the Pelles tools as linker and then it was working without using C after proto and then the return problem is also gone.So it seems to work if I enable Pelles tools but I am not sure exactly.So I would like to create a static lib and inc file which can be used for all languages if its possible so that later the user can only use my functions but I am still not sure because of this ret value issues you know.

greetz

Link to comment

in the asm file

you have the .model flat, stdcall etc and so on

compile that, it'll make the obj or the lib

in the c program in its h file

use the

#ifdef  __cplusplus
extern "C" {
#endif

DWORD __stdcall FunctionName(DWORD param);

#ifdef  __cplusplus
}
#endif

.. just to clarify.. you're writing an asm function and want to call tn i a c progam.. right or ?

Link to comment

Hi,

ok maybe I do explain it again.

So I wrote a dll with WinASM and now I wanna create a static lib from my dll file (so I dont wanna use the lib I got from WInASM after compiling so this is not a static one and if I use it in other projects then I also need to have the dll itself and this I dont wanna have so I want to have the dll code added into my new compiled files) with dll to lib tool.Now I use this new static lib in other projects and just set the functions into (API_xy PROTO C ....) but now I have this return value issues which get set by using the C after PROTO but its not needed that they set return values after my functions so they are already set inside etc you know.If I dont use C then I get unsolved error of the functions but if I enables Pelles tools in linker options then it works.

So I have only the lib created by dll to lib tool which I do include in my another projects.So all in all I could use it with enabled Pelles tools option to prevent this return value problem for myself if I use it but what about other languages if they want to use my lib so there I am also not sure whether they then also get this return value problem or not or whether they also have something to enables like Pelles if they have a option etc.

PS: Is there no way to build a static lib directly with any MASM tools etc?

greetz

Link to comment

Why do you make a static with Dll2Lib from your own dll?

You can Create static library´s with masm32

 

ml /c /coff /Cp "source.asm"

lib /VERBOSE /SUBSYSTEM:WINDOWS /OUT:"source.lib" "source.obj" @Mod.txt

 

.686
.model flat,stdcall
option casemap:none

.data

.data?

.code
;Your code here
ProcDoSomething PROC PUBLIC hWnd:HWND 
	ret
ProcDoSomething endp
END

 

  • Like 1
Link to comment

Hi @LCF-AT :

An excellent reference and answer :) to your question can be found in this thread here : Creating static libraries with MASM

An excerpt from that link :

Quote

 

Creating a statilc library is easy :
 

Code:
\masm32\bin\ml /c /coff stdout.asm
\masm32\bin\ml /c /coff strlen.asm
\masm32\bin\lib /OUT:statlib.lib stdout.obj strlen.obj


...and you get statlib.lib including the two modules stdout and strlen

Using it Visual C++ :
 

Code:
extern void __stdcall StdOut( char * );

int main()
{
  char message[]="Hello world!";
  StdOut(message);
  return 0;
}


Notice that you have to specify the right calling convention in your function declaration. Masm uses generally the stdcallconvention.

Building the project with MS Visual C++ Toolkit 2003 :
 

Code:
cl Demo.c statlib.lib

 

 
 
 
And if using the created libs  for C programs :
 
Quote

Something to remember with C++ compilers and header files....You need to make sure everything is "C" referenced style.

Example header file for a ASM function.

#if defined(__cplusplus)
extern "C" {
#endif

// ---------------------------------------------------------------------------
// Description:
//      Initializes the memory routines.
//
//      NOTE: This function must be called before any type of memory routines
//            are called. If this function isn't, all memory routines will fail.
//
// Returns:
//      Handle of the process heap or NULL if failed to get one.
// ---------------------------------------------------------------------------
HANDLE __stdcall InitializeMemoryRoutines();



#if defined(__cplusplus)
}
#endif

 

Hope that helps :D

Greetz
 

 

 

   
  • Like 1
Link to comment

oh, well the posts above cover it, try and avoid such tools, if its your source you can compile to obj or dll with no issues.. no need for 3rd party buggy stuff

Link to comment

Yes that Dll2Lib tool is VERY buggy ( I know since I used it in the past many a time).

Just check the links in my post above and you shoudl be able to compile your own static lib in MASM itself without issues :D

Greetz

Link to comment

Hi guys,

thanks for your answers so far.So now I have checked the commandline commands postet by raggy and it works. :) So I get a lib created by the MASM tools itself and now the lib code will added into compiled new files so exactly this what I want.Also now I dont get this return issues anymore and dont need to use C after PROTO or using Pelles tools etc.So I didnt know this commands before and also not that it would be possible to build such static libs with MASM tools itself and thought that I have to use this dll to lib extra tool to do such things.Anyway,again lerned something new now.

Thanks again

Link to comment

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
×
×
  • Create New...