Skip to content

Instantly share code, notes, and snippets.

@jeff123wang
jeff123wang / unloadDLL.vb
Last active April 28, 2022 14:29
When a function in the DLL is called, the DLL is loaded in memory. At that moment the DLL is "in use" and cannot be deleted or replaced. So maintenance is not possible when people are using the template.
'https://www.tek-tips.com/viewthread.cfm?qid=690668
Option Explicit
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
' example DLL call
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long
Private Sub Command1_Click()
Dim hLib As Long
@jeff123wang
jeff123wang / notification.vb
Created April 18, 2022 15:58
this will show non blocking notification using powershell in vba
'https://stackoverflow.com/questions/65680941/powershell-unable-to-find-type-windows-ui-notification
Public Function Notify(ByVal title As String, ByVal msg As String, _
Optional ByVal notification_icon As String = "Info", _
Optional ByVal app As String = "excel", _
Optional ByVal duration As Integer = 10)
'Parameters:
' title (str):Notification title
' msg (str):Notification message
@jeff123wang
jeff123wang / VBA load C DLL remotely.vb
Created April 17, 2022 14:07
another example load C_C++ dll from memory.
Private Declare PtrSafe Function MemoryLoadLibrary Lib "MemoryModule.dll" _
(lpBytes As Byte, ByVal nCount As Long) As LongPtr
Private Declare PtrSafe Function MemoryGetProcAddress Lib "MemoryModule.dll" _
(ByVal hModule As LongPtr, ByVal lpProcName As String) As LongPtr
Private Declare PtrSafe Sub MemoryFreeLibrary Lib "MemoryModule.dll" _
(ByVal hLibModule As Long)
Private Declare PtrSafe Function SetDllDirectoryA Lib "kernel32" _
Declare PtrSafe Function DispCallFunc Lib "OleAut32.dll" (ByVal pvInstance As Long, ByVal offsetinVft As Long, ByVal CallConv As Long, ByVal retTYP As Integer, ByVal paCNT As Long, ByRef paTypes As Integer, ByRef paValues As Long, ByRef retVAR As Variant) As Long
Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Const CC_STDCALL = 4
Public Function stdCallA(sDll As String, sFunc As String, ByVal RetType As VbVarType, ParamArray P() As Variant)
Dim i As Long, pFunc As Long, V(), HRes As Long
ReDim V(0)
@jeff123wang
jeff123wang / How to use a function pointer in VBA.md
Created April 10, 2022 04:02 — forked from sancarn/How to use a function pointer in VBA.md
How to use a function pointer in VBA by Akihito Yamashiro

VB6 and VBA come with no support for function pointers.

Also, when you wish to execute a function in a dll using the Declare function, you can only call functions created by the Steadcall calling conversation.

These constraints can be avoided by using the DispCallFunc API. The DispCallFunc is widely used in VB6 when erasing the history of IE. Although the DispCallFunc is known as API for calling the IUnknown interface, in fact, you can also perform other functions other than COM by passing the NULL to the first argument.

As explained in the http://msdn.microsoft.com/en-us/library/ms221473(v=vs.85).aspx , the DispCallFunc argument is as follows.

@jeff123wang
jeff123wang / ComWithoutRegister2.cls
Created April 10, 2022 03:48 — forked from relyky/ComWithoutRegister2.cls
ComWithoutRegister第二版,可真的動態載入DLL函式庫,不需註冊COM也能調用。
Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpSectionName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpbuffurnedString As String, ByVal nBuffSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpSectionName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function DispCallFunc Lib "oleaut32.dll" (ByVal pvInstance A
' https://github.com/lsimao/inflate-vba/blob/master/inflate.bas
Option Explicit
'Inflate (Deflate decompression) according https://tools.ietf.org/html/rfc1951
'TODO:
' pReadBits: MORE TESTING NEEDED!!! 1. last byte full read?, 2. pReadBit vs bit by bit, 3...
' check unused variables
' test fixed codes
' test uncompressed block
' normalize error codes
@jeff123wang
jeff123wang / mscorlib_load_assembly.vba
Created April 10, 2022 01:35 — forked from andrewchiles/mscorlib_load_assembly.vba
VBA code for calling Assembly.Load using raw vtable lookups for the IUnknown
' Need to add project references to C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscoree.tlb and mscorlib.tlb
Private Declare PtrSafe Function DispCallFunc Lib "oleaut32.dll" (ByVal pv As LongPtr, ByVal ov As LongPtr, ByVal cc As Integer, ByVal vr As Integer, ByVal ca As Long, ByRef pr As Integer, ByRef pg As LongPtr, ByRef par As Variant) As Long
Private Declare PtrSafe Sub RtlMoveMemory Lib "kernel32" (Dst As Any, Src As Any, ByVal BLen As LongPtr)
Private Declare PtrSafe Function VarPtrArray Lib "VBE7" Alias "VarPtr" (ByRef Var() As Any) As LongPtr
#If Win64 Then
Const LS As LongPtr = 8&
#Else
Const LS As LongPtr = 4&
'https://www.vbforums.com/showthread.php?329373-MsgBoxEx-Extended-Message-Box
'*************************************************************
'* MsgBoxEx() - Written by Aaron Young, February 7th 2000
'* - Edited by Philip Manavopoulos, May 19th 2005
'*************************************************************
Option Explicit
Private Type CWPSTRUCT
lParam As Long
' https://stackoverflow.com/questions/64729347/getting-a-udt-from-a-longptr-in-vba
' There are three ways to assign UDT.
' 1. Simply use = assgiment
' 2. use copymemory.
' 3. Allocate memory as a swap space, transfer the value.
' 4. in VB, you can't dereference a UDT pointer directly like in C "->"
' 5. You need to use copymemory to a UDT variable. Then use "." to reference that variable.
Type IDT
id As Long