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 621617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:53:40+00:00 2026-05-13T18:53:40+00:00

As the title suggests, I’m trying to write some C# code to interop with

  • 0

As the title suggests, I’m trying to write some C# code to interop with a C DLL (It’s a Windows device driver, basically). The error code function signature in C is:

UINT DLLErrorMsg( DWORD errorCode, char * pBuf, UINT nSize );

nSize is the size of pBuf. If nSize can fit the error message, it is copied into pBuf and 0 returned, otherwise (the buffer was too small and) the return value is the minimum size the buffer needs to be to fit the error message.

I’ve tried some variants of the following:

internal class DLLWrapper
{
    [DllImport(_libName, EntryPoint="DLLErrorMsg", CallingConvention=CallingConvention.Cdecl)]
    public static extern UInt32 GetErrorMessage(UInt32 dwError, ref char * pBuf, UInt32 nBufSize);
}

The client code at the moment looks something like this:

GetError( UInt32 errorCode )
{
    char[] errorMsg = new char[bufSize];            
    UInt32 moreChars = DLLWrapper.GetErrorMessage(errorCode, out errorMsg, bufSize);
    if (moreChars > 0)
    {
        errorMsg = new char[moreChars];
        TWLDLLInterface.GetErrorMessage(errorCode, out errorMsg, moreChars);
    }
}

But I get an exception at the call to GetErrorMessage:

An unhandled exception of type ‘System.ArgumentException’ occurred in NdevInterface.dll

Additional information: Method’s type signature is not Interop compatible.

I’ve also tried playing around with IntPtr and trying Marshal.IntPtrToStringAuto(), but that wasn’t helpful because I need to allocate the buffer, and I obviously can’t cast a char[] into an IntPtr.

I’ve tried searching through the MSDN and general internet for tips on how to do this, but most of it seems to be a little different than what I’m trying.

What am I doing wrong?

  • 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-13T18:53:40+00:00Added an answer on May 13, 2026 at 6:53 pm

    You can marshal this using a StringBuilder. For details, see this P/Invoke intro:

    [DllImport(_libName, EntryPoint="DLLErrorMsg", CallingConvention=CallingConvention.Cdecl)]
    public static extern UInt32 GetErrorMessage(UInt32 dwError, StringBuilder pBuf, UInt32 nBufSize);
    

    Just make sure the StringBuilder has been constructed with enough memory for the pBuf to get filled in.

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

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use java.util.BitSet instead. It'd be much faster than dealing with… May 14, 2026 at 6:44 pm
  • Editorial Team
    Editorial Team added an answer The $_REQUEST['url'] value is being urlencoded as opposed to urlencoding… May 14, 2026 at 6:44 pm
  • Editorial Team
    Editorial Team added an answer $ac_info is an object of AccountClass class. account_code, username, email,… May 14, 2026 at 6:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.