NO

Author Topic: some newbi questions  (Read 8585 times)

whatsup

  • Guest
some newbi questions
« 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
« Last Edit: February 07, 2010, 04:37:20 PM by whatsup »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: some newbi questions
« Reply #1 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;
}
May the source be with you

whatsup

  • Guest
Re: some newbi questions
« Reply #2 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 ?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: some newbi questions
« Reply #3 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;
}
May the source be with you

whatsup

  • Guest
Re: some newbi questions
« Reply #4 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 ?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: some newbi questions
« Reply #5 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.

May the source be with you

whatsup

  • Guest
Re: some newbi questions
« Reply #6 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.



Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: some newbi questions
« Reply #7 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.
---
Stefan

Proud member of the UltraDefrag Development Team

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: some newbi questions
« Reply #8 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".
best regards
 Alex ;)

whatsup

  • Guest
Re: some newbi questions
« Reply #9 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.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: some newbi questions
« Reply #10 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.
---
Stefan

Proud member of the UltraDefrag Development Team

whatsup

  • Guest
Re: some newbi questions
« Reply #11 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?)
« Last Edit: February 10, 2010, 09:46:05 PM by whatsup »

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: some newbi questions
« Reply #12 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 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.
« Last Edit: February 11, 2010, 10:12:45 AM by Stefan Pendl »
---
Stefan

Proud member of the UltraDefrag Development Team

whatsup

  • Guest
Re: some newbi questions
« Reply #13 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.
« Last Edit: February 12, 2010, 12:03:57 AM by whatsup »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: some newbi questions
« Reply #14 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://repo.or.cz/w/tinycc.git
« Last Edit: February 13, 2010, 01:23:03 PM by timovjl »
May the source be with you