> I have a method in C++:
>
[quoted text clipped - 6 lines]
> 1. Can anyone explain how this void method can return a new Hunspell
> object?
This method returns a 'void *'.
'void *' in C/C++ means "a pointer to something...", but what the
"something" actually is, is not specified.
I believe that the purpose of that code is to make Hunspell instances
"opaque".
Each Hunspell instance (created in the function body, using C++ operator
'new'), is "masked" using a 'void*', a void pointer, a "pointer to
something".
In 32 bits platforms, a void* is a 4-bytes (32-bits) data, which stores an
address which points to something.
I think that your library uses 'void *' (4 bytes data) to uniquelly identify
Hunspell instances. The fact that void* points to an Hunspell instance is an
implementation detail, it is hidden behind a black-box. What is seen at the
public interface is just a void*, a pointer to something.
So, I think that you will also have some functions like
hunspell_uninitialize, that may work like this:
void hunspell_uninitialize( void * x )
{
// The 'void *' is a pointer to Hunspell instance
Hunspell * hunspell = (Hunspell *)x;
delete hunspell;
}
and if Hunspell class has a method like do_something, I believe that you
will have a function like this:
void hunspell_do_something( void * x )
{
Hunspell * hunspell = (Hunspell *)x;
x->do_something();
}
i.e. you are using a code that is wrapping a C++ class (Hunspell) using a
C-like function-based interface (and the class instance is identified by
"opaque" 'void *').
> 2. Can anyone tell me how I get a reference to the Hunspell object in
> C#?
[quoted text clipped - 7 lines]
> but as you can see, I do not have any reference to the Hunspell
> object,
I think that the above C# code does not correctly map the aforementioned C++
hunspell_initialize function.
In fact the hunspell_initialize function returns 'void *', but your C# code
specifies that the return value is 'void'.
'void *' and 'void' are completely different things: 'void *' is a generic
pointer to something, instead 'void' means "nothing", "no return value".
I think that the correct mapping from C++ 'void *' in C# is 'IntPtr'.
So in your C# code you should specify that hunspell_initialze returns an
'IntPtr'.
Note that I think that you will not see any explicit reference to 'Hunspell'
class, you will just identify instances of 'Hunspell' class using 'IntPtr's.
(Of course, you may build a C# object-oriented wrapper to the C-like
function-based interface).
class Hunspell
{
// Identify current Hunspell instance
private IntPtr id_;
// Constructor - initializes Hunspell
public Hunspell( ...initialization parameters )
{
id_ = hunspell_initialize( ...init parameters ... );
}
public void DoSomething()
{
// Call C-like do_something function
hunspell_do_something( id_ );
}
...
}
HTH,
Giovanni
Joachim Van den Bogaert - 13 May 2008 08:53 GMT
Hi Giovanni,
Thanks for the explanation. This was really helpful.
If I understand correctly, I return an IntPtr whenever C++ returns
void *.
As for the rest of my code I will have to define some structs that map
the classes that are being used in the C++ code.
I found most what I need here:
http://msdn.microsoft.com/en-us/library/eshywdt7(printer).aspx
Regards,
Joachim
Giovanni Dicanio - 13 May 2008 09:28 GMT
> Thanks for the explanation.
You're welcome.
> If I understand correctly, I return an IntPtr whenever C++ returns
> void *.
Yes, I think that in your sample code you should "map" C++ 'void *' with
IntPtr in C#.
Giovanni