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

  • Home
  • SEARCH
  • 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 3426782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:45:26+00:00 2026-05-18T06:45:26+00:00

I’ve totally failed this really simple P/Invoke. [System.Runtime.InteropServices.DllImport( temp.dll )] private static extern System.IntPtr

  • 0

I’ve totally failed this really simple P/Invoke.

    [System.Runtime.InteropServices.DllImport(
        "temp.dll"
    )]
    private static extern System.IntPtr parser_alloc();

    [System.Runtime.InteropServices.DllImport(
        "temp.dll", 
        CharSet = System.Runtime.InteropServices.CharSet.Ansi
    )]
    private static extern void parser_add_file(
        System.IntPtr p,
        [MarshalAs(UnmanagedType.LPStr)]string f,
        [MarshalAs(UnmanagedType.LPStr)]string s);

    [System.Runtime.InteropServices.DllImport(
        "temp.dll"
    )]
    private static extern void parser_free(
        System.IntPtr p);

    [System.Runtime.InteropServices.DllImport(
        "temp.dll"
    )]
    private static extern void parser_emit_results(
        System.IntPtr p);

    [System.Runtime.InteropServices.DllImport(
        "temp.dll",
        CharSet = System.Runtime.InteropServices.CharSet.Ansi
    )]
    private static extern void parser_file_updated(
        System.IntPtr p,
        [MarshalAs(UnmanagedType.LPStr)]string f,
        int offset,
        int added);

Creating the object seems to go fine. However, when I try to call parser_add_file, then the runtime tells me I have a mismatched signature. This is my existing C++ code:

class Parser {
public:
    void add_file(std::string filename, std::string source) {
        std::cout << "add_file | " << filename << " | " << source << "\n";
    }
    void clear_contents() {
        std::cout << "clear_contents\n";
    }
    void emit_results() {
        std::cout << "emit_results\n";
    }
    void file_updated(std::string filename, int offset, int added) {
        std::cout << "file_updated | " << filename << " | " << offset << " | " << added << "\n";
        // added should be negative to signify removing
    }
    Parser() {
        std::cout << "I made a Parser!\n";
    }
};

extern "C" __declspec(dllexport) Parser* parser_alloc() {
    return new Parser;
};

extern "C" __declspec(dllexport) void parser_add_file(Parser* p, const char* filename, const char* string) {
    p->add_file(filename, string);
}

extern "C" __declspec(dllexport) void parser_free(Parser* p) {
    delete p;
}

 extern "C" __declspec(dllexport) void parser_emit_results(Parser* p) {
    p->emit_results();
}

 extern "C" __declspec(dllexport) void parser_file_updated(Parser* p, const char* filename, int offset, int added) {
    p->file_updated(filename, offset, added);
}

Should I just skip P/Invoke and go to C++/CLI for this job?

  • 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-18T06:45:27+00:00Added an answer on May 18, 2026 at 6:45 am

    Turns out that I manually had to set the calling convention to cdecl- the default is stdcall.

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

Sidebar

Related Questions

No related questions found

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.