Click here to Skip to main content
15,888,610 members
Home / Discussions / C#
   

C#

 
GeneralRe: Posting an article to a blog from C# Pin
Ravi Bhavnani6-Feb-06 2:37
professionalRavi Bhavnani6-Feb-06 2:37 
GeneralRe: Posting an article to a blog from C# Pin
AB77716-Feb-06 2:50
AB77716-Feb-06 2:50 
GeneralRe: Posting an article to a blog from C# Pin
Ravi Bhavnani6-Feb-06 4:02
professionalRavi Bhavnani6-Feb-06 4:02 
GeneralRe: Posting an article to a blog from C# Pin
AB77716-Feb-06 18:56
AB77716-Feb-06 18:56 
AnswerRe: Posting an article to a blog from C# Pin
Ravi Bhavnani7-Feb-06 2:08
professionalRavi Bhavnani7-Feb-06 2:08 
QuestionExcel SaveAs() method Pin
Sandrina5-Feb-06 23:54
Sandrina5-Feb-06 23:54 
QuestionCan We place RadioButtons in DataGridView Pin
USrinivas5-Feb-06 23:43
USrinivas5-Feb-06 23:43 
Questionp/invoke c++ classes with virtual functions Pin
mintguy5-Feb-06 23:01
mintguy5-Feb-06 23:01 
I have a bit of a problem trying to use a C++ class that contains virtual functions though C# p/invoke. Here is a trivial simplification of the problem to illustrate the issue.

With Visual Studio.Net 2003:

If I have a C++ class like so:

class ClassA
{
    ClassA(){n = 0;}
    int n;
    void F(){n++;}
};


And an exported function that uses this class like so:

extern "C"
{
    __declspec(dllexport) void ExportedFunc(ClassA** ppA)
    {
        ClassA* pA = *ppA;
        pA->F();
        }
    }


I can call into this from C# by declaring a mapped structure for marshalling like so:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public class ClassA
{
    int n;
}


And a function to wrap the exported C function:
class DLLImports
{
    [DllImport("ExportFuncs.dll",
        EntryPoint="ExportedFunc",
        SetLastError=true,CharSet=CharSet.Unicode,
        ExactSpelling=true,
        CallingConvention=CallingConvention.StdCall)]
    public static extern void ExportedFunc(ref ClassA refA);
}


I can then make a call into the dll containing the exported function like so:
{
    .
    .

    ClassA refA = new classA();
    DLLImports.ExportedFunc(ref refA);
    .
    .
}


Okay so this works a treat.

But if I now modify classA so this it contains a virtual function like so:
class ClassA
{
    ClassA(){n = 0;}
    virtual ~ClassA(){}
    int n;
    void F(){n++;}
};


We run into a problem. The C++ class ClassA now has an extra four bytes at the beginning to contain the vTable pointer. So this needs to be taken into account when creating the structure layout in C#. Now I have tried just putting a dummy four btyes in at the beginning and I have tried changing the class to LayoutKind.Explicit and
using the FieldOffset to offset the first item in the class by four bytes, but I still find that when the code gets into the C function the vtable pointer has been reset to null.

Is there a way to overcome the problem or is is simply a case of "don't use classes that contain a virtual function with p/invoke?

Regards

M
AnswerRe: p/invoke c++ classes with virtual functions Pin
leppie6-Feb-06 0:59
leppie6-Feb-06 0:59 
GeneralRe: p/invoke c++ classes with virtual functions Pin
mintguy6-Feb-06 22:06
mintguy6-Feb-06 22:06 
GeneralRe: p/invoke c++ classes with virtual functions Pin
leppie6-Feb-06 22:15
leppie6-Feb-06 22:15 
QuestionHow to use Pay Pal Pro [Urgent] Pin
Virendrak5-Feb-06 22:44
Virendrak5-Feb-06 22:44 
AnswerRe: How to use Pay Pal Pro [Urgent] Pin
exhaulted6-Feb-06 0:27
exhaulted6-Feb-06 0:27 
QuestionMDI data storage in XML Pin
bharath_kraj5-Feb-06 22:34
bharath_kraj5-Feb-06 22:34 
AnswerRe: MDI data storage in XML Pin
exhaulted6-Feb-06 0:26
exhaulted6-Feb-06 0:26 
GeneralRe: MDI data storage in XML Pin
bharath_kraj7-Feb-06 2:41
bharath_kraj7-Feb-06 2:41 
QuestionInstalling Windows Services. Double Quotes in Executable File Pin
dmivlad5-Feb-06 22:07
dmivlad5-Feb-06 22:07 
QuestionPlease help - How to get the Path of Executable of a Windows Service... Pin
Tigger995-Feb-06 21:56
Tigger995-Feb-06 21:56 
QuestionDisplaying the system menu Pin
AB77715-Feb-06 21:11
AB77715-Feb-06 21:11 
AnswerRe: Displaying the system menu Pin
exhaulted6-Feb-06 0:35
exhaulted6-Feb-06 0:35 
GeneralRe: Displaying the system menu Pin
AB77716-Feb-06 0:41
AB77716-Feb-06 0:41 
QuestionVirtual Directory in Web Application Pin
raheeli5-Feb-06 20:02
raheeli5-Feb-06 20:02 
AnswerRe: Virtual Directory in Web Application Pin
leppie5-Feb-06 20:48
leppie5-Feb-06 20:48 
Questionimplementing ajax Pin
sharma sanjeev5-Feb-06 19:52
sharma sanjeev5-Feb-06 19:52 
AnswerRe: implementing ajax Pin
osamahmirza9-Feb-06 17:45
osamahmirza9-Feb-06 17:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.