Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4610298
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:06:41+00:00 2026-05-22T01:06:41+00:00

in my C# code I want to import a C++ DLL. I use the

  • 0

in my C# code I want to import a C++ DLL. I use the dllimport and it works fine with a some of the functions. But in one function I get a HANDLE which I need later to call another function.

   [DllImport("SiUSBXp.dll")]
   public static extern int SI_Open(UInt32 deviceNum,ref IntPtr devHandle );   // this function gets the HANDLE
   [DllImport("SiUSBXp.dll")]
   public static extern int SI_Write([In]IntPtr devHandle, [In, Out] byte[] inputByte, UInt32 size,ref UInt32 bytesWritten); // this function needs the HANDLE

In my code these functions are called like this:

   IntPtr devHandle = new IntPtr();
   UInt32 bytesWritten = new UInt32();
   byte[] byteArr = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   SI_Open(0, ref devHandle);
   SI_Write(devHandle, byteArr, 10, ref bytesWritten);

If I do it like this I get an “System.AccessViolationException”. I searched here and in the internet but didnt find a specific answer. How do I use the IntPtr correctly, so it works?
Best Regards

Toby

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:06:42+00:00Added an answer on May 22, 2026 at 1:06 am

    Your SI_Write function looks quite like Windows Kernel32’s WriteFile.

    So, I would do this:

    [DllImport("SiUSBXp.dll", SetLastError = true)]
    static extern int SI_Open(uint dwDevice, ref IntPtr cyHandle);  
    
    [DllImport("SiUSBXp.dll", SetLastError = true)]
    static extern int SI_Write(IntPtr cyHandle, byte[] lpBuffer,
       uint dwBytesToWrite, out uint lpdwBytesWritten);
    

    EDIT: I found this documentation USBXPRESS® PROGRAMMER’S GUIDE on the web, and it states that the SI_Write prototype looks actually much closer to WriteFile than I thought. The doc states this:

    SI_STATUS SI_Write (HANDLE Handle, LPVOID Buffer, DWORD NumBytesToWrite,
    DWORD *NumBytesWritten, OVERLAPPED* o = NULL)
    

    It means the .NET prototype should be this instead:

    [DllImport("SiUSBXp.dll")]
    static extern int SI_Write(IntPtr Handle, byte[] Buffer,
       uint NumBytesToWrite, out uint NumBytesWritten, IntPtr o);
    

    o is optional so you can pass IntPtr.Zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add QSplashScreen to PyQT4 application. It works fine from Python, but
I want to import image file into JSF page. I use this code: <div
I've import dll in my code like that: [DllImport(dll.dll, CharSet = CharSet.Ansi)] private static
I have the following sample code which doesn't seem to want to run. import
I have the following code I want to run, but the problem is $this->type
I'm writing some semi-portable code and want to be able to detect when I'm
I have a git repo, and a revision code I want it to get
Here is my source code I'm trying to get to work: In Main.hs: import
In fact, I got a C++ (working) DLL that I want to import into
I have a third party c dll which I want to use in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.