Native ANSI C library from .NET Framework >= 4 on 64 Bit

Pascal

I have a C# application that (P/)invokes an ANSI C library, provided by the manufacturer, to access an RFID-Reader over the network. This works absolutely fine with .NET Framework versions 2.0 up to 4.5 if I manually set the platform to x86.

With the platform set to 64 Bit on the other hand, it only works with .NET Framework versions <= 3.5. The error code I get is defined as "InvalidHandle", where the handle is an int I'm given by the library identifying the reader. I don't get what an impact the Framework Version could have on a native library, especially if only so on a 64 Bit platform.

What I've observed, although I'm not sure if it is relevant at all: - In the case of failure, the handle is 10 digits long, sometimes positive, sometimes negative - In the case of success, the handle is 9 digits long, always positive

Based on this observation I've tried other data types (int, uint, long, ulong) without success (and with the same behaviour).

The "setup" of the component consists of multiple steps (method calls), which are: 1. Init (where I get the handle from) 2. SetChannel (takes a byte as a parameter) 3. SetAddress (takes a connection string containing an IP and a Port as parameter) 4. Open() Note: All those methods take the handle as an int out-parameter. Because the method SetAddress fails, when Init and SetChannel also take the handle as a parameter but don't, I suspect it might have something to do with the string, although I haven't found any evidence to support this theory.

Unfortunately the documentation is close to non-existent.

So any ideas are greatly appreciated. If you need more information, I'd be glad to provide it.

Regards,

Pascal

[DllImport("/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
static extern int BLUEBOX_Init(out int Handle);

[DllImport("/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
static extern int BLUEBOX_SetAddress(ref int Handle, byte Address);

[DllImport("/x64/BLUEBOXLib.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
static extern int BLUEBOX_SetChannel(ref int Handle, String Channel, String Settings);

Excerpt from the BLUEBOXLib.h file:

BLUEBOXLib_API BLUEBOX_ErrorCodes __stdcall BLUEBOX_Init (BLUEBOX_Handle *Handle);

BLUEBOXLib_API BLUEBOX_ErrorCodes __stdcall BLUEBOX_SetChannel (BLUEBOX_Handle *Handle, char *Channel, char *Settings);

BLUEBOXLib_API BLUEBOX_ErrorCodes __stdcall BLUEBOX_SetAddress (BLUEBOX_Handle *Handle, unsigned char Address);

typedef int BLUEBOX_Handle;
Hans Passant
    typedef int BLUEBOX_Handle;

That is a problem, one you cannot fix because it doesn't appear in your code. There are multiple ways to implement a handle but they basically fall into two distinct categories. Either it a simple index into an internal array or it is a plain pointer. When you see "10 digits long, sometimes positive, sometimes negative" then it is 99% odds for a pointer.

That pointer takes 8 bytes in 64-bit mode, it cannot fit in an int. And yes, this sometimes still works, it depends on the operating system and the simplicity of the test program. On the x64 version of Vista or Win7, a 64-bit pointer can still fit in a 32-bit value by accident, allocations often happen in the lower 2 GB address range. You'll have zero odds for success on Win8, also a hint why it might seem to work on .NET 3.5. And yes, negative values won't work because they'll sign-extend when the C code casts the handle to the real pointer type, producing a garbage pointer value.

You cannot fix this bug, it has to be fixed by the vendor. They have to change the typedef to void*, now you can use IntPtr on your end. Give them a call. If they are unresponsive, then consider running this code in a separate 32-bit helper process that you interop with.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using 32 bit library from 64 bit project - .NET

From Dev

Error while calling C# from java using jni4net from 64 bit Os

From Dev

Calling C++ 64bit shared library from Python

From Dev

EasyHook: Inject 64 bit dll from 32 bit app in native C app?

From Dev

Use 64-bit library from 32-bit application

From Java

Can't call C standard library function on 64-bit Linux from assembly (yasm) code

From Dev

Communicating with 32 bit Access from 64 bit .NET

From Dev

Which .net framework (32 or 64 bit) would an app use on a 64 bit windows if it was built with 'Any Cpu' config

From Java

Can I place both the 32bit and 64bit versions of a native library on java.library.path?

From Dev

Using 32-bit library from 64-bit application with perl or neko

From Dev

.NET Framework 4 Library with .NET Core 2.2 Project

From Dev

an application required libstdc++ for GLIBCXX_3_4_9 library on RHEL 5 64bit system

From Dev

Eclipse 4-10 64bit: Cannot resolve SWT library

From Dev

ODBC .NET 32 bit and 64 bit

From Dev

How is a header only ANSI C library possible?

From Dev

Calling C functions from julia on windows 7 (64bit)

From Dev

Passing pointer from visual C to Fortran and back after changing code from 32 bit to 64 bit

From Dev

DPI-1047: Cannot locate a 64-bit Oracle Client library from nodejs code

From Dev

Diagnosing DllNotFoundException when using a native Library from .net UWP app

From Dev

procontroll library not working on windows 7 , 64 bit

From Dev

Generate a 64 bit .so file of GDAL Library

From Dev

Will the pyodbc library work for 64-bit Windows?

From Dev

C# login bot, .Net Framework 4

From Dev

F# System.Runtime.InteropServices native library call to SendInput works in .net framework but not in .net core

From Dev

Having problems registering and using a 64-bit C++ written ATL dynamic library

From Dev

going from 32 bit to 64 bit in assembly

From Dev

moving from ubuntu 32 bit to 64 bit

From Java

Linking 32-bit library to 64-bit program

From Dev

Error while installing 32 bit Library on 64 bit Ubuntu

Related Related

  1. 1

    Using 32 bit library from 64 bit project - .NET

  2. 2

    Error while calling C# from java using jni4net from 64 bit Os

  3. 3

    Calling C++ 64bit shared library from Python

  4. 4

    EasyHook: Inject 64 bit dll from 32 bit app in native C app?

  5. 5

    Use 64-bit library from 32-bit application

  6. 6

    Can't call C standard library function on 64-bit Linux from assembly (yasm) code

  7. 7

    Communicating with 32 bit Access from 64 bit .NET

  8. 8

    Which .net framework (32 or 64 bit) would an app use on a 64 bit windows if it was built with 'Any Cpu' config

  9. 9

    Can I place both the 32bit and 64bit versions of a native library on java.library.path?

  10. 10

    Using 32-bit library from 64-bit application with perl or neko

  11. 11

    .NET Framework 4 Library with .NET Core 2.2 Project

  12. 12

    an application required libstdc++ for GLIBCXX_3_4_9 library on RHEL 5 64bit system

  13. 13

    Eclipse 4-10 64bit: Cannot resolve SWT library

  14. 14

    ODBC .NET 32 bit and 64 bit

  15. 15

    How is a header only ANSI C library possible?

  16. 16

    Calling C functions from julia on windows 7 (64bit)

  17. 17

    Passing pointer from visual C to Fortran and back after changing code from 32 bit to 64 bit

  18. 18

    DPI-1047: Cannot locate a 64-bit Oracle Client library from nodejs code

  19. 19

    Diagnosing DllNotFoundException when using a native Library from .net UWP app

  20. 20

    procontroll library not working on windows 7 , 64 bit

  21. 21

    Generate a 64 bit .so file of GDAL Library

  22. 22

    Will the pyodbc library work for 64-bit Windows?

  23. 23

    C# login bot, .Net Framework 4

  24. 24

    F# System.Runtime.InteropServices native library call to SendInput works in .net framework but not in .net core

  25. 25

    Having problems registering and using a 64-bit C++ written ATL dynamic library

  26. 26

    going from 32 bit to 64 bit in assembly

  27. 27

    moving from ubuntu 32 bit to 64 bit

  28. 28

    Linking 32-bit library to 64-bit program

  29. 29

    Error while installing 32 bit Library on 64 bit Ubuntu

HotTag

Archive