Creating Cursor

I am writing a program to create a window on WinAPI. And there was a problem with creating a cursor (which is why the window have busy cursor when launched). There should be a macro, but Swift does not support it
My creation function

	func registerClass(name: LPCSTR) {
		var wndClass = WNDCLASSEXA()
    	wndClass.cbSize = UINT(MemoryLayout<WNDCLASSEXA>.size)
    	wndClass.style = UINT(CS_HREDRAW | CS_VREDRAW)
    	wndClass.lpfnWndProc = WindowProc
    	wndClass.cbClsExtra = 0
    	wndClass.cbWndExtra = 0
    	wndClass.hInstance = hInstance
    	wndClass.hIcon = LoadIconA(nil, "32512")
    	wndClass.hCursor = LoadCursorA(nil, "32512")
    	wndClass.hbrBackground = CreateSolidBrush(0x00d9d9d9)
    	wndClass.lpszMenuName = nil
    	wndClass.lpszClassName = name
    	wndClass.hIconSm = nil
    	guard RegisterClassExA(&wndClass) != 0 else {
    		print("can't register class\n \(GetLastError())")
        	return
    	}
	}

Problem in this strings

    	wndClass.hIcon = LoadIconA(nil, "32512")
    	wndClass.hCursor = LoadCursorA(nil, "32512")

Its normal (c/c++) must be like this

        wndClass.hIcon = LoadIconA(nullptr, IDI_APPLICATION);
        wndClass.hCursor = LoadCursorA(nullptr, IDC_ARROW);

But IDI_APPLICATION and IDC_ARROW is macro then mean MAKEINTRESOURCE(32512) and next MAKEINTRESOURCE(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
With string code compile and work but window is busy and clas no have cursor.
Function has prototype

public func LoadCursorA(_ hInstance: HINSTANCE!, _ lpCursorName: LPCSTR!) -> HCURSOR!

but use function argument how with class name (in func registerClass(name: LPCSTR)) not working
Need for solution convert Int32 number 32512 to UnsafePointer<Int8>!

This not working

"32512".withCString{ ... }

The MAKEINTRESOURCE macro is casting i (in this case 32,512) to a WORD (uint16_t) then casting the word to a ULONG_PTR (uintptr_t (with some caveats)) then casting that to a LPSTR (which on any current hardware is just a char*). You're definitely passing a char*, but it's to the wrong string.

Try this:

let IDI_APPLICATION = UnsafePointer<CChar>(bitPattern: 32_512)!
let IDC_ARROW = UnsafePointer<CChar>(bitPattern: 32_512)!

// ...
        wndClass.hIcon = LoadIconA(nil, IDI_APPLICATION)
    	wndClass.hCursor = LoadCursorA(nil, IDC_ARROW)
// ...
1 Like

Thank you very much. It works.