473,395 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DLL-P/Invoke to C++ class

I've read the posts about instantiating a class from a dll in C# and
then even accessing the class method via the
CallingConvention=CallingConvention.ThisCall. Now I have a Class
defined in the header dll which has a pure virtual method. Inside the
dll a specific class is created which then implements the pure
virtual. The base class, defined in the header is being returned to
the C# program. I cannot get the EntryPoint (via Depends) to any pure
virtual function. And if I give the base class a default
implementation, the call from the C# app will call the base
implementation and not the derived implementation. Any ideas?

i.e. The following will return 1 instead of 2. And I don't know how
to get 3

DllHeader.h
-----------
class BaseClass
{
virtual int PureVirtual_Method() = 0;
virtual int Virtual_Method();
}
extern "C" STP_API BaseClass * CreateSubClass();

DllHeader.cpp
-------------
int BaseClass::Virtual_Method() { return 1; }
extern "C" STP_API BaseClass * CreateSubClass()
{
return new SubClass();
}

InternalHeader.h
----------------
class SubClass
{
virtual int PureVirtual_Method();
virtual int Virtual_Method();
}

InternalHeader.cpp
------------------
int SubClass::PureVirtual_Method() { return 3; }
int SubClass::Virtual_Method() { return 2; }

Class.cs
--------
public class BaseClass : IDisposable
{
private IntPtr _this = IntPtr.Zero;

public BaseClass()
{
_this = CreateSubClass();
}

public int Virtual_Method()
{
return CppClass_Virtual_Method(_this);
}

[DllImport(@"CppClass.dll")]
private static extern IntPtr CreateSubClass();

[DllImport(@"CppClass.dll", EntryPoint="#1",
CharSet=CharSet.Unicode,
CallingConvention=CallingConvention.ThisCall)]
private static extern int CppClass_Virtual_Method(IntPtr
instance);
}
Nov 16 '05 #1
2 7131
Jeffrey,

If you can modify the C++ code, I'd add a function like this

extern "C" STP_API int CallMethod(BaseClass *p)
{
return p->PureVirtual_Method();
}

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Well great minds think alike. I started doing that soon after I posted
the message. That seems like the only way to get either the virtual or
pure virtual to call the SubClass. This means though that if you don't
have the source code (luckily I do, although I'm trying to maintain a
common interface for C++ lib interface and C# P/Invoke interface) you
will have to write a wrapper class or dll to interface. Thanks for the
response though.

Jeff

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stefan | last post by:
Hi, I just downloaded PHP-4.3.3 for win32 and got the extensions folder and dropped it intot he root of the PHP directory and set it's path in the PHP.ini: extension_dir =...
3
by: Matthias Baas | last post by:
Hi, are there any guidelines about what to do if a Windows extension for Python 2.4 requires the C++ runtime (msvcp71.dll)? If I want to distribute a binary installer of an extension that...
24
by: Toby Mathews | last post by:
Hi there, I have recently started having problems compiling multi-project solutions. I have a couple of ASP.Net projects that reference a range of .Net DLLs I have written. For example: ...
14
by: Neil Ginsberg | last post by:
I am using VBA/Access to send SMTP mail using CDO. I set a reference to "Microsoft CDO for Windows 2000 Library" (cdosys.dll). Everything works fine, both on my PC and on another PC. However, on...
3
by: Wiktor Zychla | last post by:
I have a problem I cannot solve. My application hosts IE activex control. I follow the standard procedure: I just aximp shdocvw.dll. Note that this gives you two files: axshdocvw.dll and...
4
by: Matt Sawyer | last post by:
I am attempting to use an API (CxApiOem.dll) that has a large number of defines and complicated structs. It's just too much hassle to attempt to use DLLImport to make the desired API calls. ...
11
by: Vijay Chegu | last post by:
I have built a 64bit ATL COM dll. When i register the dll on IA64 windows Enterprise server 2003, i get following error. DllRegisterServer in mydll.dll failed. Return code was : 0x80020009 ...
3
by: Benjamin Harper | last post by:
Hi, I've recently upgraded a mixed-mode MFC DLL of mine from VS2003 to VS2005 using the newer /clr language. In the process I also removed the manual entry point stuff that used to be necessary....
22
by: Brett Romero | last post by:
If my UI app uses three DLLs and two of those DLLs reference something named utilities.dll, does the UI app load utilities.dll twice or does the compiler recognize what is going on and load...
0
by: HeroOfSpielburg | last post by:
Hello, I'm new to the realm of .NET application creation, so please forgive my ignorance. I have been tasked with debugging a problem that arises with using a DLL that contains managed and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.