-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Successfully @cImport("Windows.h") #3453
Comments
I've given this another go with 0.5.0+c4770e7aa. const windows = @cImport({
@cDefine("WIN32_LEAN_AND_MEAN", "1");
@cInclude("Windows.h");
});
usingnamespace windows;
extern "user32" fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCSTSTR, uType: UINT) callconv(.Stdcall) c_int;
export fn WinMain(hInstance: HINSTANCE, hPrevInstace: HINSTANCE, lpCmdLine: PWSTR, nCmdShow: INT) callconv(.Stdcall) INT {
MessageBoxA(null, "Hello", "title", 0);
}
I'm not sure what libc headers have to do with finding and importing Windows.h. |
did you try |
I have not, but why should that be required to include Windows.h? |
I'm guessing windows.h uses some things from libc. I tried cIncluding a minimal .h file with no #includes and was able to build-exe without -lc |
I'm sure Windows.h does use CRT stuff, but could you explain why that matters for importing? Maybe I should re-name the title of this issue as I'm only really concerned about the Win32 ramifications of this problem. And unfortunately Windows.h is probably the most insane C header in existence. |
@cImport from: https://ziglang.org/documentation/master/#cImport :
So when you use @cImport / @Cinclude("Windows.h"), zig will parse Windows.h and everything it includes. One thing that may be informative is to run |
Thanks. I do know the general mechanism, although I don't see how that has to do with the linker / libc, especially if lazy analysis is involved. But this is all quite complex so that's why I ask. I returned to this issue after seeing the flurry of translate-c work done recently. I'll see what translate-c does in hopes that illuminates the core of the issue after work. |
Windows.h immediately includes winapifamily.h, which Zig translate cannot find (even though it's in the /share folder just above). Even tried running Zig translate inside the Developer Command Prompt for VS 2019.
|
You'll need to -I..\share. Check the output of
also |
Oops. Thanks for that. I'll see how far that gets me. The Windows.h is going to pull in files from who knows where in general. Is it expected that all Win32 dev's provide the full include list or is @cImport() eventually going to handle that itself? |
I don't think the list of includes is actually that long. I think you just have to include the include paths of the windows SDK - the path where you found Windows.h. Might want to try getting an example C program compiling from the command line using cl.exe (Microsoft/visual studio compiler), then port that. I'm about 80% sure you have to manually add the windows SDK when compiling from the command line with cl.exe, too |
That's what I'm doing. =) But with Win32 dev, you plunge immediately into the deep end of the pool by basically requiring Windows.h right off the bat except for the most basic programs. A simple MessageBoxA call is easy to port over, but when you want a proper window responding to keyboard / mouse events, you have to dig into Win32 structures instead of just type aliases.
That's taken care of by executing cl.exe inside the Developer Command Prompt, but it's not necessary for Clang. Since Clang can do it without fuss, I would hope that Zig can. |
Your file contents appear to be in utf16? How did you create that file? Perhaps zig should emit a warning/note if file contents looks like a utf16 BOM |
Ah, I bet that's it. I just piped the output of zig translate > Windows.zig, like that. I'll save as UTF-8 tomorrow morning to see if that fixes it. |
Yes that'll be it - done it myself |
That fixed it. Also, after the usual win32 shenanigans, I have a window properly registered and displayed, all through Zig! This doesn't close the issue though. There's quite a few declarations in Windows.zig (when created via the manner discussed in this thread) that are not properly handled. Not sure how to enumerate the issues that are useful for others that care about this, as they are very obvious to anyone who will attempt to register and display a basic window like I did here. So I've just been selectively moving what bits and pieces I need over, which isn't much. |
Nice job. For the unhandled declarations I would suggest starting with the first error you see. See if you can create a new issue with a minimal reproduction of c code which results in the mis-translation. |
Just translated the header to Zig, and I get conversion mistakes like these: pub const LCS_sRGB = 'sRGB'; These come from typedef enum {
LCS_CALIBRATED_RGB = 0x00000000,
LCS_sRGB = 0x73524742,
LCS_WINDOWS_COLOR_SPACE = 0x57696E20
} LogicalColorSpace; Arguably a very ugly way to encode a small string, and although it does correctly convert as if it were a kind of string, it should be written as |
No, that comes from #define LCS_sRGB 'sRGB' The macro-handling part in |
Actually the multiple characters in a character literal is a Microsoft extension:
From: https://docs.microsoft.com/en-us/cpp/cpp/string-and-character-literals-cpp?view=vs-2019 " So in a way it is a character literal. ;)
No, it should be an integer value. Maybe |
For anyone who ended up in this issue because they were interested in using Win32, there is https://github.com/GoNZooo/zig-win32 I've had no massive issues creating Win32 apps with this, including registering window classes, handling events, etc. |
Hello, I'm including libusb and vJoy to write a vJoy feeder on pub const GetCurrentFiber = @compileError("unable to translate function"); // C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\..\um\winnt.h:22637:1
// C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\..\um\winnt.h:22637:1
pub const NtCurrentTeb = @compileError("unable to translate function"); // C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\..\um\winnt.h:22627:1
// C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\..\um\winnt.h:22643:19: warning: TODO implement translation of CastKind BuiltinFnToFnPtr
pub const GetCurrentFiber = @compileError("unable to translate function"); // C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\..\um\winnt.h:22637:1
pub fn GetFiberData() callconv(.C) PVOID {
return @ptrCast([*c]PVOID, @alignCast(@alignOf(PVOID), GetCurrentFiber())).?.*;
} This causes problems with
Any way to work around this? EDIT: It seems this is the only remaining issue importing |
I'm now having a problem with the semicolon after |
Update: I've tried again after updating to master and the issue I mentioned earlier seems to be fixed after some recent commits! |
When translating
|
There should now be no rendering issues so if someone on windows can confirm it then this issue should be closed and any remaining problems should be made into their own issues.
This is #4071.
This is #4479. |
main.zigconst c = @cImport(@cInclude("windows.h"));
pub fn main() void {
_ = c;
} Compiles succesfully with |
A substantial milestone for translate-c is the successful c-importing of Windows.h.
The text was updated successfully, but these errors were encountered: