Skip to content

Instantly share code, notes, and snippets.

@CarlTBarnes
Last active July 5, 2020 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlTBarnes/9fcde37b3b5278083cb594244fb9361f to your computer and use it in GitHub Desktop.
Save CarlTBarnes/9fcde37b3b5278083cb594244fb9361f to your computer and use it in GitHub Desktop.
LoadLibrary Example
!--Step 1.-- Define MAP with Function that has DLL(1) or DLL(_fp_), and must have NAME() that matches LONG in step 2
MAP
GetProcsByName PROCEDURE(BYTE ShowErrors=0),BOOL
MODULE('Win32')
SetWindowDisplayAffinity PROCEDURE(SIGNED hWnd, UNSIGNED dwAffinity),BOOL,PROC,PASCAL,DLL(_fp_),NAME('SetWinDspAff')
! ^^^^ ^^^^^^^^^^^^
LoadLibraryA PROCEDURE(*CSTRING pszModuleFileName),LONG,RAW,PASCAL,DLL(1)
FreeLibrary PROCEDURE(LONG hModule),BOOL,PASCAL,DLL(1),PROC
GetModuleHandleA PROCEDURE(*CSTRING lpModuleName),LONG,RAW,PASCAL,DLL(1)
GetProcAddress PROCEDURE(long HInstance,*cstring ProcName),LONG,PASCAL,RAW,DLL(1)
GetLastError PROCEDURE(),LONG,PASCAL,DLL(1)
END
MODULE('RTL')
!Alexey said to use these for Clarion built DLLs. I think these do AttachToThread() from RTL.
Clw_LoadLibrary PROCEDURE(*CSTRING pszModuleFileName),LONG,PASCAL,RAW,DLL(dll_mode),NAME('Clw$LoadLibrary')
Clw_FreeLibrary PROCEDURE(LONG hModule),LONG,PASCAL,PROC,DLL(dll_mode),NAME('Clw$FreeLibrary')
END
END
!--Step 2.-- Define LONG with same NAME()
SetWinDspAff_fp LONG,NAME('SetWinDspAff')
! ^^^^^^^^^^^^ MUST match Name() in Procedure(). Thank you Larry Sand!
!--Step 3.-- Define Function to LoadLibrary and GetProc. I know User32.DLL is loaded so used GetModuleHandleA
GetProcsByName PROCEDURE(BYTE ShowErrors=0)!,BOOL
hDll LONG,AUTO
hProc LONG,AUTO
DllName CSTRING('user32.dll')
ProcName CSTRING('SetWindowDisplayAffinity')
CODE
IF ~SetWinDspAff_fp THEN
SetWinDspAff_fp=-1 !set to -1 if failed to Get
hDll=GetModuleHandleA(DllName)
IF hDll THEN
hProc=GetProcAddress(hDll, ProcName)
IF hProc THEN
SetWinDspAff_fp=hProc
ELSIF ShowErrors THEN
Message('GetProcsByName GetProcAddress(' & hDll &','& ProcName &')||Error ' & GetLastError())
END
ELSIF ShowErrors THEN
Message('GetProcsByName GetModuleHandleA(' & DllName &')||Error ' & GetLastError())
END
END
RETURN CHOOSE(SetWinDspAff_fp<>-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment