I am trying to write a binding for www.xcgui.com which is a GUI lib for windows. The initial code is on https://github.com/retsyo/xcgui_nim
but I met some questions. for example
import ../../xcgui
import winim/inc/[windef, wingdi]
import winim/winstr
import sugar
proc OnClick(bHandle: var bool): cint =
var
sel = XC_MessageBox(nil, "炫彩界面库pText", "pCaption", 1)
bHandle = true
return 0
proc Main() =
#~ type ptrFnCallBack = (proc(bHandle: var bool): cint)
#~ type ptrFnCallBack = (cint - > cint) # sugar does not work
#~ var fnCallBack: ptrFnCallBack = OnClick
var
bOk = XInitXCGUI()
if bOk == True:
var
hWindow = XWnd_Create(0, 0, 300, 200, "炫彩NIM例子", 0, xc_window_style_default)
if hWindow != nil :
var hBtn = XBtn_Create(8, 30, 100, 20, "点我", hWindow)
if hBtn != nil :
XEle_RegEventC( hBtn, XE_BNCLICK, cast[cstring](OnClick))
XWnd_ShowWindow(hWindow, 5)
XRunXCGUI()
else:
echo "a"
XExitXCGUI()
if isMainModule:
Main()
will crash if I click the button
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: 'E:\my_project\nim\xcgui_\nim_\Cpp\002buttonevent\WinMain.exe '
can someone help me to fix it? thanks
XEle_RegEventC( hBtn, XE_BNCLICK, cast[cstring](OnClick))
Casting onClick proc to cstring? I may vote for removing cast operation from Nim.
There's a complete wrapper in Golang already it might help you with developing a nim version: https://github.com/CodyGuo/xcgui
Will you also write some kind of documentation ?
I have found that no mmatter the is cast[cstring] or not, the compiled app still crash after I click the button and the msgbox shows
here is the line in C header
XC_API BOOL WINAPI XEle_RegEventC(HELE hEle, int nEvent, void *pFun);
here is what VB code does
Public Function OnClick(ByRef bHandle As Long) As Long
MsgBox "炫彩界面库"
bHandle = True
OnClick = 0
End Function
Private Sub Main()
XEle_RegEventC hBtn, XE_BNCLICK, AddressOf OnClick
End Sub
I don't know Go-lang, and I don't have time to learn it. As I always state, I do not make a living on programing, I only program in my spare time, sometimes I make some app to help me or co-worker in job. That is why I use Python mainly because Python is simple and has tons of libraries which almost meets what I need.
But since some bundled exe file from python source file via pyinstaller/py2exe failed to run on every PCs, I try to use nim to make app with little or no extra dependencies, the other reason why I choose nim is because nim looks like python. But it seems this is a tough choice.
So if possible, please make some help to improve nim's weak ecological environment. I don't have too much time and experience in programing
As for the document, the binding use XCGUI function directly, if you need you should read XCGUI's manual which is supplied by www.xcgui.com
Look up the setupForeignThreadGc proc.
https://nim-lang.org/docs/system.html#setupForeignThreadGc.t%2C
This conversation also might help.
I don't have too much time and experience in programing
Without time and experience it is really not easy I guess. Maybe you can hire someone?
Unfortunately I do know nothing about VirtualBasic. Maybe the calling convention of your OnClick proc is wrong? For C procs I used cdecl pragma, you may need one also, maybe a different one. You get the crash when you click a button, so I would guess that your onClick proc is one problem. Can you pass nil when you register that onclick proc? When that is possible, and no crash occurs, then you may know that that proc is the problem. Maybe you can remove the proc body from onclick proc, so that it immediately returns for testing. And test various calling conventions.
I have the good will and the readiness to help and even some experience with Nim interfacing C. What I do not have, however, is the willingness to read through more than a couple of (relevant!) lines of code, to wrap my head around whole projects of users needing help and in particular I am absolutely positively not ready to invest more time and effort than the one asking a question.
I'm sorry to word it that rude but it's not really news. It has been written a thousand times in diverse IT forums of diverse kinds. "Do not just mindlessly plunk a rather unspecific question, in particular questions like "It doesn't work!" into a forum and expect help!
First rule: make it easy to help you. Help others to help you! Second rule: think at the very minimum enough about your problem so as to be able to word it clearly and concisely! Btw, the reward for that effort will surprisingly often be that you actually don't need help anymore (or way less help) because trying to present a problem well often leads to seeing a solution.
do you mean
proc OnClick*(bHandle: var bool): cint {.exportc, cdecl.} =
setupForeignThreadGc()
var
sel = XC_MessageBox(nil, "炫彩界面库pText", "pCaption", 1)
bHandle = true
result = 0
tearDownForeignThreadGc()
it still crashes after the msgbox appears
btw, I have add ` --threads:on --tlsEmulation:off` to compile the source