Skip to content

linking issue with xinput DLL #1763

Open

Description

@andrewrk

@errpr reports the following problem:

<errpr> I'm having a problem. My program never halts if I link to window's xinput1_4 DLL
<errpr> this is my code
const std = @import("std");

const XINPUT_GAMEPAD = struct {
    wButtons: c_ushort,
    bLeftTrigger: u8,
    bRightTrigger: u8,
    sThumbLX: c_short,
    sThumbLY: c_short,
    sThumbRX: c_short,
    sThumbRY: c_short,
};

const XINPUT_STATE = struct {
    dwPacketNumber: c_ulong,
    Gamepad: XINPUT_GAMEPAD,
};

extern "xinput1_4" stdcallcc fn XInputGetState(dwUserIndex: c_ulong, pState: *XINPUT_STATE) c_ulong;

pub const HINSTANCE__ = extern struct {
    unused: c_int,
};
pub const HINSTANCE = ?*HINSTANCE__;

pub export fn WinMain(Instance: HINSTANCE, PrevInstance: HINSTANCE, lpCmdLine: ?[*]u8, nCmdShow: c_int) c_int 
{
    var ControllerState: XINPUT_STATE = undefined;
    const errorcode = XInputGetState(0, &ControllerState);
    
    return 0;
}

We determined, using --verbose-link to find out the linker line, and replacing LLD with link.exe, that the problem goes away with microsoft's linker. So this appears to be an LLD issue.

We need to create a nice bug report for LLD upstream so that they fix it. The sooner the better, to maximize the chance that the fix comes out in 8.0.0, in time for zig 0.4.0.

This issue will remain open until the problem is solved in Zig master branch.

Activity

  1. added
    bug
    contributor friendly
    os-windows
    upstream
    on Nov 20, 2018
  2. added this to the 0.4.0 milestone on Nov 20, 2018
  3. modified the milestones: 0.4.0, 0.5.0 on Mar 1, 2019
  4. modified the milestones: 0.5.0, 0.6.0 on Apr 30, 2019
  5. modified the milestones: 0.6.0, 0.7.0 on Feb 10, 2020
  6. modified the milestones: 0.7.0, 0.8.0 on Oct 30, 2020
  7. modified the milestones: 0.8.0, 0.9.0 on Nov 6, 2020
  8. modified the milestones: 0.9.0, 0.10.0 on May 19, 2021
  9. jhark commented on Jun 4, 2024

    @jhark

    Doesn't reproduce with Zig 0.13.0-dev.351+64ef45eb0, Windows 10.

    The test program also gets pad state as expected.

    zig build-exe -lc -lXInput1_4 main.zig

    Updated code:

    const std = @import("std");
    
    pub const XINPUT_GAMEPAD = extern struct {
        wButtons: c_ushort,
        bLeftTrigger: u8,
        bRightTrigger: u8,
        sThumbLX: c_short,
        sThumbLY: c_short,
        sThumbRX: c_short,
        sThumbRY: c_short,
    };
    
    pub const XINPUT_STATE = extern struct {
        dwPacketNumber: c_ulong,
        Gamepad: XINPUT_GAMEPAD,
    };
    
    pub extern fn XInputGetState(c_ulong, [*c]XINPUT_STATE) c_ulong;
    
    pub fn main() void {
        var state = std.mem.zeroes(XINPUT_STATE);
        const err = XInputGetState(0, &state);
        std.debug.print("state: {}\nerr: {}\n", .{ state, err });
    }

    (using @cImport(@cInclude("XInput.h")); also works just fine)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcontributor friendlyos-windowsupstream

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions