Pelles C forum

C language => Beginner questions => Topic started by: whatsup on February 07, 2010, 04:32:13 PM

Title: some newbi questions
Post by: whatsup on February 07, 2010, 04:32:13 PM
- PellesC doesn't have switch to output Assembler Code ?

when i want to call a Windows API (MessageBox)
- how the compiler know what dll to use (in case i don't use Pelles libs)? how can i declare the dll ?

- i saw a source code that the programmer load common dll "wininet.dll" to use some internet functions,
is this necacerry with all APIs ?
doesn't the Windows loader do that when it execute an app (according to the header information of the EXE file) ?

- i want to run a simple app,
with only 2 lines

MessageBox("hy") ...
TerminateProcess ...

how can i do this with no need to link to any libs ?
just to declare these 2 functions, and their dll,
and on run time, windows will load this 2 funcs, and that's it,
i don't want any other lib code to do the load or any kind of wrapping for me.
is it possible at ?




thanks in advanced
Title: Re: some newbi questions
Post by: TimoVJL on February 07, 2010, 06:55:02 PM
Empty program for start.
Add LoadLibrary etc.

Code: [Select]
//#define WIN32_DEFAULT_LIBS
//#define WIN32_LEAN_AND_MEAN
//#include <windows.h>

//#pragma lib "kernel32.lib" // for LoadLibrary
//#pragma lib "user32.lib" // for MessageBox

int __cdecl WinMainCRTStartup(void)
{
return 0;
}
Title: Re: some newbi questions
Post by: whatsup on February 07, 2010, 08:03:11 PM
thanks man, but i don't understand,
why do i need this function ? is that because pelles link me to a library that calls this func ?
or is it because windows calls this function ? (which i don't think so)

i simply want to do it like that:
in the linker options i will set an entry point
it can be any function name i want
and in that function i will do all the init steps needed.

second.
do i have to load the dll myself ? i thought it's the OS loader which retrieve the info from
the EXE header,
i mean , the linker write in the EXE header, which dlls and functions i need,
and on run time, Windows loader load these dlls for me,
isn't it so ?
Title: Re: some newbi questions
Post by: TimoVJL on February 07, 2010, 11:34:23 PM
If you want to bypass init code here is an example:
This code links user32.lib with that #pragma lib
That WinMainCRTStartup() is an internal entry point for linker.

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma lib "user32.lib" // for MessageBox

int __cdecl WinMainCRTStartup(void)
{
MessageBox(0, "Test", "Test", MB_OK);
return 0;
}
Title: Re: some newbi questions
Post by: whatsup on February 08, 2010, 03:49:18 AM
thank you very much, so now i understand that the pragma is the way to tell the linker what dll i need.

so why do i see sometimes code that uses loadlibrary to load a dll ?
maybe it's because it's a control dll ?
Title: Re: some newbi questions
Post by: TimoVJL on February 08, 2010, 10:58:54 AM
#pragma lib is just an another way to include lib.

Normally the libraries are included projects linker options/commandline.
In POIDE: Project -> Project options... -> Linker -> Library and object files:

Sometimes dll is loaded dynamically with LoadLibrary when it is used occasionally
or just when needed or depending current language.

Title: Re: some newbi questions
Post by: whatsup on February 08, 2010, 06:52:14 PM
sorry but i made a mistake, i thought the pragma is for dll,
now i see it's for lib

and:

1. i want to know how to tell the linker that i need a dll not a lib
2. i already said that i don't want to use any library,
only dll that will be loaded by the OS loader on executing my app.


Title: Re: some newbi questions
Post by: Stefan Pendl on February 08, 2010, 10:38:21 PM
The library is needed to automatically load the DLL at runtime.

The library includes the information about which DLL to load at runtime.
Title: Re: some newbi questions
Post by: AlexN on February 09, 2010, 08:07:24 AM
- PellesC doesn't have switch to output Assembler Code ?
Pelles C has switches to show the assemler-output /Tx86-asm, /Tarm-asm and /Tamd64-asm. If you use one of this switches, you should rename also the output-file (/Fo) because the compiler gives the output-file the default-extension ".obj".
Title: Re: some newbi questions
Post by: whatsup on February 09, 2010, 11:55:04 PM
Thank you AlexN i'll check that.

Stefan, i thought the Linker put in the EXE file info about which dll to load,
and the OS loader does the loading, no need for lib/user function,
so i'll have to check that.
Title: Re: some newbi questions
Post by: Stefan Pendl on February 10, 2010, 06:01:53 PM
Stefan, i thought the Linker put in the EXE file info about which dll to load,
and the OS loader does the loading, no need for lib/user function,
so i'll have to check that.

Yes, the linker includes the DLL dependency in the executable, but he does not know this, if the LIB file is not used to tell him about this.
Title: Re: some newbi questions
Post by: whatsup on February 10, 2010, 09:41:38 PM
but in other languages like pascal, basic,
they have an option to declare the exact dll

so C doesn't have this option ?

or maybe

windows loader doesn't load the dll for me ,
and i need to do it my self, or with the help of some lib function or API. (loadlibrary?)
Title: Re: some newbi questions
Post by: Stefan Pendl on February 11, 2010, 10:10:31 AM
C does work very different compared to other languages.
Some languages have inherited the way C works, while others have used different approaches.

See the CreateWindow function at MSDN (http://msdn.microsoft.com/en-us/library/ms632679(VS.85).aspx) as an example of how C works.
At the bottom of each of the API function definitions you will find a box containing the important information to use the function in C/C++.

Quote
Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systemsWindows 95, Windows NT 3.1
UnicodeImplemented as ANSI and Unicode versions.

Why do all the things manually inside the code, if it can be done automatically by setting the projects properties.
I see no reason to make the code more complicated, than it needs to be.

Do not try to make C work like VB or any other language.
It is C, so you will have to adapt to how it works.

You will find that there are many languages and they all work in different ways, so you will always have to learn how they work and not make them work as any other language you already know.



If you still want to do all this manually, you will have no choice than using LoadLibrary.
Find examples at MSDN.
Title: Re: some newbi questions
Post by: whatsup on February 12, 2010, 12:00:24 AM
i didn't say i want to do, because i don't know yet exactly how to do.
but what i want is to know exactly all the details, how it works,
and how can i use dll, without use any lib func,
even if i'll need to load it manually with loadlibrary.
after i'll know the exact details, i can decide :)

and the details i miss are, how other languages load dlls,
do they use loadlibrary, or what i thought from the begining,
they only put information on the EXE header, and windows loader load it.
and if this is true, i really wonder why C can't do that , if it really can't.
Title: Re: some newbi questions
Post by: TimoVJL on February 12, 2010, 06:29:22 PM
Here is old example, you can study it.

Using TestDll:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

//#define DYNDLL
int SampleFunction(int, int);

#ifdef DYNDLL
typedef int (WINAPI SAMPLEFUNCTION)(int a, int b);
typedef SAMPLEFUNCTION *LSAMPLEFUNCTION;
#else
#pragma comment(lib, "TestDll.lib")
#endif

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
#ifdef DYNDLL
HANDLE hLib;
static LSAMPLEFUNCTION SampleFunction;
hLib = LoadLibrary("TestDLL.DLL");
SampleFunction = (LSAMPLEFUNCTION)GetProcAddress(hLib, "SampleFunction");
#endif
SampleFunction(0, 0);
#ifdef DYNDLL
FreeLibrary(hLib);
#endif
return 0;
}

Making TestDll:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

BOOL __stdcall DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;

case DLL_THREAD_ATTACH:
break;

case DLL_THREAD_DETACH:
break;

case DLL_PROCESS_DETACH:
break;
}

return TRUE;
}

int __declspec( dllexport ) __stdcall SampleFunction(int a, int b)
{
MessageBox(0, "SampleFunction", "TestDLL", MB_OK);
return a * b;
}

PS:
TinyCC,s internal linker can use .def and .dll directly without lib-file, so it is possible that linker can support that.

http://bellard.org/tcc/ (http://bellard.org/tcc/)
http://repo.or.cz/w/tinycc.git (http://repo.or.cz/w/tinycc.git)
Title: Re: some newbi questions
Post by: whatsup on February 13, 2010, 07:33:34 PM
thanks for reply.

i see what you did, you simply used the loadlibrary,
but that's exactly the point i want to figure out,
is this the only way to load a dll (without lib func)
or
there is a way that windows loader will load it for me, according the data in the EXE header.

if the second is not possible in C, s this possible in other languges ?

for example in Assembler or Pascal

if i want to use a dll func in these langs without linking any Lib,
do i have to use loadlibrary, or windows loader will do that for me ?
Title: Re: some newbi questions
Post by: Stefan Pendl on February 13, 2010, 08:19:04 PM
If you do not allow the linker to know the dependencies, then there is nothing put in the dependency table of the executable.

If you do not like to change the linker properties of the project properties, then you will not have any other way, than getting the address of the function manually as shown in the example.
Title: Re: some newbi questions
Post by: whatsup on February 13, 2010, 11:41:04 PM
so do you say that the only way to tell the linker the dependecies, is to link it to a lib ?

that was my point,
the dependecies are the name of the func and the dll , right ?

so that's my question, how to write the dll name in C,
from this discussion i understand that this is impossible,

where in basic for example it's very easy:

Code: [Select]
declare sub foo lib "kernel.dll"


if what i said so far is true, then it's a great disappoint that this little dll name, can't be include
in the func proto in C.

another question:
the lib that has the dll name, has also code to load it ? or only its name ?