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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:21:30+00:00 2026-06-04T20:21:30+00:00

in c++ i have these below classes class A { public: int __thiscall check(char

  • 0

in c++ i have these below classes

class A
{
  public: 
      int __thiscall check(char *x,char *y,char *z);
  private:
      B *temp;
};

class B
{
  friend class A;
  Public:
    B();
    B(string x,string y,string z);
    ~B();
  private:
    string x;
    string y;
    string z;
};

my dll method in c++ is like this

__declspec(dllexport) int __thiscall A::check(char *x,char *y,char *z)
{
  temp=new B(x,y,z);
  return 1;
}

code for B() constructor is below:

B::B(string x, string y,string z)
{
  .......
}

below mentioned is my c# dll import

[DllImport("sour.dll", CallingConvention = CallingConvention.ThisCall, ExactSpelling = true, EntryPoint = "check")]
    public static extern void check(IntPtr val,string x,string y,string z);

the c++ build went successful with out any errors, but when i call this method from c# using dll import method i am getting this below error when i am trying to assign memory for “temp” class pointer. Below mentioned is the error.

 Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Can any one please help on this. Thanks in advance.

  • 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-06-04T20:21:31+00:00Added an answer on June 4, 2026 at 8:21 pm

    I would assume exporting methods instead of class members is easier.
    Please make sure that the DllImport uses the correct calling convention.

    __declspec(dllexport) void __cdecl check(int x)
    

    Using PInvoke Interop Assistant results in

    public partial class NativeMethods {
    
        /// Return Type: void
        ///x: int
        [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="check", CallingConvention=System.Runtime.InteropServices.CallingConvention.Cdecl)]
    public static extern  void check(int x) ;
    
    }
    

    Did you make sure that 32/64 bit version of your dll matches. If you run the x86 .net version (32bit) you can activate debugging of native and managed code and you should be able to set a break point in your C++ method to see what happens.

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

Sidebar

Related Questions

I have these classes in C# (.NET Framework 3.5) described below: public class Base
I have these classes: public class Product { [Key] public virtual int ProductId {
I have these two classes public class Person { } public class Company {
I have these two classes: class Student { String name; String age ; }
I have 3 classes with below definition public class UserRole { public User User
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have an interface defined as below: public interface TestInterface{ int id { get;
I have classes as below. public interface ITest <T> { public Set<T> methodHere(); }
Let's say you have a class class C { int * i; public: C(int
I have a Userdetails class like the one below public class UserDetails { public

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.