CAN_SetValue returns Unknown Error

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
phyllostachys
Posts: 2
Joined: Fri 4. Apr 2025, 21:56

CAN_SetValue returns Unknown Error

Post by phyllostachys » Thu 8. May 2025, 19:49

I was trying to implement the event system on Windows in a Rust library that wraps the PCANBasic API. I only get back Unknown Error (0x10000) whenever I make this call. Snippet of code looks like this, copying from the C++ example.

Code: Select all

                // HANDLE iBuffer = CreateEvent(NULL, FALSE, FALSE, L"");
                let event_handle: windows::Win32::Foundation::HANDLE =
                    unsafe { CreateEventA(None, false, false, s!("")).unwrap() };

                // TPCANStatus stsResult = CAN_SetValue(PcanHandle, PCAN_RECEIVE_EVENT, &iBuffer, sizeof(iBuffer));
                let status: PcanStatus = unsafe {
                    dbg!(PCAN_LIBRARY.CAN_SetValue(
                        channel,
                        PCAN_RECEIVE_EVENT as u8,
                        event_handle.0,
                        std::mem::size_of::<windows::Win32::Foundation::HANDLE>() as u32,
                        // std::mem::size_of_val(&event_handle) as u32,
                    ))
                }.into();
PCAN_LIBRARY is just a struct with function pointers loaded from the DLL. channel is just the usual PCAN_USBBUS2 (0x52). The HANDLE in the windows crate is just a thin wrapper around a usual void pointer (that's what's returned by the .0 notation).

Is there anything obvious that I'm doing wrong?

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: CAN_SetValue returns Unknown Error

Post by K.Wagner » Fri 9. May 2025, 07:55

Hello,

as we (still) do not work or know Rust programming, it is difficult to say something about the construtct of your code, but for us it looks like you would use the value of the event handle and not a reference to it.

Additionally, an ERROR_UNKNOWN is normally returned, when an internal exception is rised. If you activate the log functionality (PCAN_LOG_STATUS), then you should see an entry for the exception when it coccurrs and a WIndows error code. This should help you further to find the problem.
Best regards,
Keneth

phyllostachys
Posts: 2
Joined: Fri 4. Apr 2025, 21:56

Re: CAN_SetValue returns Unknown Error

Post by phyllostachys » Fri 9. May 2025, 20:39

While trying to enable logging, I realized that Buffer is supposed to be the address of the thing storing the parameter setting. I think I got hung up because it's a void pointer and the thing we were trying to pass in is a void pointer. It's obvious why this is wrong when you are trying to set something that's just a value using the PCAN_SetValue function (such as PCAN_LOG_STATUS to PCAN_PARAMETER_ON).

Code: Select all

                // TPCANStatus stsResult = CAN_SetValue(PcanHandle, PCAN_RECEIVE_EVENT, &iBuffer, sizeof(iBuffer));
                let status: PcanStatus = unsafe {
                    PCAN_LIBRARY.CAN_SetValue(
                        channel,
                        pcan_sys::PCAN_RECEIVE_EVENT as u8,
                        addr_of_mut!(event_handle.0) as *mut c_void,				//  <-- get address of HANDLE, which is a void** and we further cast it as just a void*
                        std::mem::size_of::<windows::Win32::Foundation::HANDLE>() as u32,	// still works!
                    )
                }
                .into();

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: CAN_SetValue returns Unknown Error

Post by K.Wagner » Mon 12. May 2025, 09:20

Hello,

ok, this is what I stated in my last post:
K.Wagner wrote:
Fri 9. May 2025, 07:55
it looks like you would use the value of the event handle and not a reference to it
But, I cannot understand from your code if it is working now or not? despite of this, please note, that what the function expects is a "buffer", meaning, the value you want to set as a reference (the address of a variable containing the needed value).
Pointer.png
Pointer.png (14.34 KiB) Viewed 2866 times
There are already one or two wrappers for PCAN-Basic for Rust in the internet (not from PEAK). Maybe you want to use one of them? For example, peak-can-sys
Best regards,
Keneth

Post Reply