-
Notifications
You must be signed in to change notification settings - Fork 561
Add dll example #818
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
Add dll example #818
Conversation
examples/dll/src/lib.rs
Outdated
#[no_mangle] | ||
#[allow(non_snake_case)] | ||
#[allow(unused_variables)] | ||
pub extern "stdcall" fn DllMain(module: HINSTANCE, reason: u32, reserved: *mut c_void) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples/dll/src/lib.rs
Outdated
pub extern "stdcall" fn DllMain(module: HINSTANCE, reason: u32, reserved: *mut c_void) -> bool { | ||
match reason { | ||
DLL_PROCESS_ATTACH => unsafe { | ||
MessageBoxA(None, "Hello from the DLL!", "Hello World", MB_OK); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DllMain
runs under the loader lock, making lots of things problematic, like "call[ing] functions in User32.dll or Gdi32.dll". A safe alternative for MessageBoxA
would be OutputDebugStringA.
examples/dll/src/lib.rs
Outdated
_ => (), | ||
} | ||
|
||
true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be adjusted to match the correct return type (see above).
I'm not convinced that this example is complete enough. The primary reason to produce a DLL is to have clients consume it. In its current shape, the DLL doesn't export any useful functionality, and there is no client code to consume the library either. The latter is particularly interesting, and how this should be done is still under investigation. It's usually a good idea to submit an issue to discuss the scope and implementation of an example before pushing a PR. |
Thanks for the feedback Tim. You are indeed right about BOOL and Previously for testing I used a basic program that called Lastly I switched from MessageBox to In hindsight: Yes, an issue should have been created to discuss the details. |
You can of course create a DLL with Rust, but that does not require anything from the Windows crate. Once I have #81 completed, we should be able to add a component example that illustrates how to build a DLL that actually implements various kinds of APIs. Until then I don't think it will be beneficial to add a DLL example. By the way, writing your own |
Hello,
I have created an example for a Windows DLL that displays a messagebox upon being loaded.
Extra notes:
Looking into the window-rs documentation I was not able to locate DWORD nor LPVOID for DllMain so I opted to check the winapi crate, and found:
u32
*mut c_void