kernel32 usage question

Windows specific forum
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

kernel32 usage question

Post 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()
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: kernel32 usage question

Post 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"!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
xorc1zt
Enthusiast
Enthusiast
Posts: 276
Joined: Sat Jul 09, 2011 7:57 am

Re: kernel32 usage question

Post 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.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: kernel32 usage question

Post by RichAlgeni »

Understood, thanks!
Post Reply