#Include "window9.bi"

Using GDIPLUS
Dim shared ULONG_PTR_01 AS ULONG_PTR
DIM GDIPLUSSTARTUPINPUT_01 AS GDIPLUSSTARTUPINPUT
GDIPLUSSTARTUPINPUT_01.GdiplusVersion = 1
IF (GDIPLUSSTARTUP(@ULONG_PTR_01, @GDIPLUSSTARTUPINPUT_01, NULL) <> 0) THEN
	PRINT "FAIL"
EndIf

Function LoadImageFromResourceA(lpResName As LPCTSTR) As Any Ptr Export
	Dim As HINSTANCE hInst
	Dim As PVOID pImage
	Dim As HRSRC hrsrc
	Dim As HGLOBAL hResGlob
	Dim As DWORD SizeRes
	Dim As LPBYTE pRes
	Dim As IStream ptr pStream
	Dim bmp As HBITMAP

	hInst=GetModuleHandle(0)
	bmp=LoadImage(hinst,lpResName,IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR Or LR_DEFAULTSIZE)
	If bmp=0 Then
		bmp=LoadImage(hinst,lpResName,IMAGE_ICON,0,0,LR_CREATEDIBSECTION Or LR_DEFAULTSIZE )
		If bmp = 0 Then
			hrsrc = FindResource(hInst,lpResName,RT_RCDATA)
			If hrsrc=0 Then Return 0
			hResGlob = LoadResource(hInst, hrsrc)
			SizeRes = SizeofResource(hInst, hrsrc)
			pRes = LockResource(hResGlob)
			Dim As HGLOBAL hgMem = GlobalAlloc(GMEM_MOVEABLE,SizeRes)
			If hgMem=0 Then Return 0
			Dim As LPBYTE pMem = GlobalLock(hgMem)
			CopyMemory(pMem, pRes, SizeRes)
			GlobalUnlock(hgMem)
			FreeResource(hResGlob)
			If (CreateStreamOnHGlobal(hgMem,TRUE,@pStream))<>0 Then
				GlobalFree(hgMem)
				Return 0
			EndIf
			GdipLoadImageFromStream(pStream,@pImage)
			GlobalFree(pMem)
			GlobalFree(hgMem)
		Else
			GdipCreateBitmapFromHICON(Cast(Hicon,bmp), @pImage)
		endif
	Else
		GdipCreateBitmapFromHBITMAP(bmp, NULL, @pImage)
		DeleteObject(bmp)
	EndIf
	return pImage
End Function

