Page 1 of 1

kernel32 usage question

Posted: Thu Sep 13, 2012 9:46 pm
by RichAlgeni
I've been trying to determine the best way to use kernel32, should it be opened as a dynamic link library? Should it be imported as a static library? Or, as it is part of the Windows API, can I just use it's functions by appending an underline character?

I wrote and compiled these 3 programs to see if any act differently, and was surprised to see the one that opened the dll, the .exe file was twice as large as the code importing kernel32.lib, and the code using the API. I thought it's .exe would be smaller, since it used a dll. There seems to be no other than that, that I can tell. I guess my question is, which is the best way, if there is a best way?

This .exe was 8704 bytes in size

Code: Select all

EnableExplicit

Global dll_kernel32 = OpenLibrary(#PB_Any, "kernel32.dll")

Procedure Proc1()
    Protected snapShot.i
    Protected Proc32.PROCESSENTRY32

    If dll_kernel32
        snapShot = CallFunction(dll_kernel32, "CreateToolhelp32Snapshot", #TH32CS_SNAPPROCESS, 0)
        If snapShot
            Proc32\dwSize = SizeOf(PROCESSENTRY32)
            If CallFunction(dll_kernel32, "Process32First", snapShot, @Proc32)
                While CallFunction(dll_kernel32, "Process32Next", snapShot, @Proc32)
                    Debug Str(PeekI(@Proc32\th32ProcessID)) + "       " + PeekS(@Proc32\szExeFile)
                Wend
            EndIf
            CloseHandle_(snapShot)
        EndIf
    EndIf
EndProcedure

Proc1()

Delay(2000)

Proc1()

CloseLibrary(dll_kernel32)
This .exe was 4096 bytes in size

Code: Select all

EnableExplicit

Import "kernel32.lib"
    CreateToolhelp32Snapshot(var1.i, var2.i)
    Process32First(var1.i, var2.i)
    Process32Next(var1.i, var2.i)
EndImport

Procedure Proc1()
    Protected snapShot.i
    Protected Proc32.PROCESSENTRY32

    Proc32\dwSize = SizeOf(PROCESSENTRY32)
    
    snapShot = CreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0)
    If snapShot
        If Process32First(snapShot, @Proc32)
            While Process32Next(snapShot, @Proc32)
                Debug Str(PeekI(@Proc32\th32ProcessID)) + "       " + PeekS(@Proc32\szExeFile)
            Wend
        EndIf
        CloseHandle_(snapShot)
    EndIf
EndProcedure

Proc1()

Delay(2000)

Proc1()
This .exe was 4906 bytes in size

Code: Select all

EnableExplicit

Procedure Proc1()
    Protected snapShot.i
    Protected Proc32.PROCESSENTRY32

    Proc32\dwSize = SizeOf(PROCESSENTRY32)
    
    snapShot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
    If snapShot
        If Process32First_(snapShot, @Proc32)
            While Process32Next_(snapShot, @Proc32)
                Debug Str(PeekI(@Proc32\th32ProcessID)) + "       " + PeekS(@Proc32\szExeFile)
            Wend
        EndIf
        CloseHandle_(snapShot)
    EndIf
EndProcedure

Proc1()

Delay(2000)

Proc1()

Re: kernel32 usage question

Posted: Thu Sep 13, 2012 9:58 pm
by ts-soft
Kernel32.lib is always linked to all windowsprograms!
It makes no difference if you use the functions with underscore or import it.
If you load the dll dynamic, the difference is only the linked Library "Library"!

Re: kernel32 usage question

Posted: Sun Sep 16, 2012 8:09 am
by xorc1zt
first one is larger because OpenLibrary, CallFunction, ... are functions from a purebasic library wich must be included into the executable code. also note that kernel32.lib is not a static lib but a import lib. the dynamic linking informations are wrote into the import table of the executable by the linker when you compile the code. the dynamic link will be automatically done at the process initialization when you run the executable. one of the advantage to do the link yourself is to load/unload the dll when you want.

Re: kernel32 usage question

Posted: Sun Sep 16, 2012 7:18 pm
by RichAlgeni
Understood, thanks!