Extern Function Name

BlitzMax Forums/BlitzMax Programming/Extern Function Name

Nigel Brown(Posted 2005) [#1]
Am trying to find the correct name to use as Extern "Win32" for FormatMessage? I get an error: undefined reference to `FormatMessage@24' from the linker when using the Function name 'FormatMessage'. Is there a way of telling what the windows name really is?

FormatMessageA and FormatMessageW give the same errors


boomboommax(Posted 2005) [#2]
downloads the api-guide, good app will show ya all that stuff


BlitzSupport(Posted 2005) [#3]
The example below works for me. As far as I can see, you can't use a 'pretty' name (eg. FormatMessage instead of FormatMessageA), but have to use the actual function name, unlike the second example below (Mark?)...

(This version gets the function address from "...\BlitzMax\lib\libkernel32.a".)

Const FORMAT_MESSAGE_ALLOCATE_BUFFER = 256
Const FORMAT_MESSAGE_IGNORE_INSERTS = 512
Const FORMAT_MESSAGE_FROM_STRING = 1024
Const FORMAT_MESSAGE_FROM_HMODULE = 2048
Const FORMAT_MESSAGE_FROM_SYSTEM = 4096
Const FORMAT_MESSAGE_ARGUMENT_ARRAY = 8192

Extern "win32"
	Function FormatMessageA (flags, source:Byte Ptr, messageid, languageid, buff:Byte Ptr, buffsize, args [])
End Extern

' -----------------------------------------------
' Example...
' -----------------------------------------------

error = 2 ' "File not found" error code...

Local message:Byte [255]

FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM, Null, error, 0, message, 255, Null)

Print ""
Print "Error message: " + String.FromCString (message)


Alternative version, accessing DLL directly...

Const FORMAT_MESSAGE_ALLOCATE_BUFFER = 256
Const FORMAT_MESSAGE_IGNORE_INSERTS = 512
Const FORMAT_MESSAGE_FROM_STRING = 1024
Const FORMAT_MESSAGE_FROM_HMODULE = 2048
Const FORMAT_MESSAGE_FROM_SYSTEM = 4096
Const FORMAT_MESSAGE_ARGUMENT_ARRAY = 8192

Global FormatMessage (flags, source:Byte Ptr, messageid, languageid, buff:Byte Ptr, buffsize, args []) "win32"

lib = LoadLibraryA ("kernel32.dll")

If lib
	FormatMessage = GetProcAddress (lib, "FormatMessageA")
Else
	Print "Dead"; End
EndIf

' -----------------------------------------------
' Example...
' -----------------------------------------------

error = 2 ' "File not found" error code...

Local message:Byte [255]

FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, Null, error, 0, message, 255, Null)

Print ""
Print "Error message: " + String.FromCString (message)


This little routine can be pretty handy if you're writing Win32 API stuff and a function fails for no discernible reason (use Win32\kernel32's GetLastError function for the error code)...

The API Guide Mr C refers to is probably this one:
http://www.mentalis.org/agnet/apiguide.shtml