ERRORS SetOption(
int nOption, String ^strSetting){
return (ERRORS) _SetOption(m_Handle, nOption, strSetting);}
The native function accepts a "void *" as it's last argument. This could be an integer, string or even a function callback depending on the nOption parameter. How would I wrap this The problem is that I cannot convert strString to a void *.
Would it be possible to use a more "lowlevel" type as the input instead of "String ^" Does something like "void *" exist in a managed version The closest thing I can think of is something like a generic "object" but my knowledge is pretty weak in this area.
Suggestions are welcome.
Thanks in advance.
-- Henrik

String ^ to void *
davecarrera
As said in the earlier post I already use enum classes. However advice on where to put these flags (options, errorcodes, etc.) would be useful.
In the ideal case I'd like to put them in the same place. Some of the flags are auto-generated by a utility (created by me too). This auto-generated code I can change to output as I like but it makes it hard to put the information in just one place (simply because the auto-generate is in a seperate file from the non-generated code).
In the ideal case I'd like to do:
my_namespace
{
myclass
{
all flags here.
}
}
But this could only be done if enum classes could be anonymous. The alternative would be to do inheritance and output everything as integers which is then made into this class.
Example (C++):
class Auto_generated
{
public:
static int blah =0;
static int blah2 =1;
};
class my_wrapper : public Auto_generated
{
... the auto-generated code is part of my class now.
};
However in this case I would loose type information.
Right now I have errorcodes as:
typedef unsigned long MY_ERROR_TYPE;
Since you cannot do typedef's in .NET (as far as I know) you would have to make this data as integers.
I'm just looking for bestpractices since there are obviously many ways you can solve this. Having the user type less and more clear type information is always prefered.
Thanks in avance :-)
-- Henrik
Gimly
pin_ptr takes onlythe type it points to. So the syntax is correct. Note that the object is pinned only for the lifetime of the pin_ptr. After the function SetOption exits the pointer you passed to _SetOption gets invalid!
Seraph Jiang
I agree that you shouldn't use #define's!
#defiens can not make use of namespaces, because they are interpreted by the preprocessor. Use const, enum's, or static values in a class. I prefer enum's!
Creating structs inside a class makes sense but it depends on the logic were they are used.
Michel Peres
CString cstr(strSetting);
return (ERRORS) _SetOption(m_Handle, nOption, static_cast<LPCTSTR>(cstr));
This might complain about not able to convert a LPCSTR to a void*, than add a const_cast<void*>(static_cast<LPCTSTR>(cstr)).
This will only work, if _SetOption reads the string, but does not change it.
If it changes the string you should use GetBuffer() on cstr to get a writeable pointer. Before returning you have then to assign the string back to strSetting.
Regards,
Bernd
wirol
The original code has alot of errorcodes written as as defines like:
#define BLAH_SUCCESS 0
#define BLAH_FAILURE_SOME_REASON 1
#define BLAH_FAILURE_SOME_OTHER_REASON 2
...
I want to make this C++/CLI wrapper usable by C# and VB.NET. Therefore I need to wrap all those errors into a "managed version" of this. I did this by using enum classes. All this is handled under a custom namespace.
Since I have not worked very much previously with .NET I have some questions in that regard:
What would be most natural to write (wrappering the enum class within the main wrapper class or only within the namespace):
if (function() != my_namespace.ERRORS.BLAH_SUCCESS)
...
or
if (function() != my_namespace.mainclass.ERRORS.BLAH_SUCCESS)
...
Similarly the settings for the function are written in the enum class SETTINGS.
Also I have some C struct's which needs to be wrapper into a .NET class (to be used as parameters with some function calls). Does it make sense to create it as a inner class or just a seperate one within the namespace
These are not language specific questions but more design guidelines.
Good suggestions or best practice are welcome.
seemay
The answer the questions: the function accepts ascii and will only read the string.
I tried to use pin_ptr in the following way (which seems to work):
ERRORS SetOption(SETTINGS nOption, Object ^o)
{
pin_ptr<void> pO = &o; return (ERRORS) _SetOption(m_Handle, (int) nOption, pO);}
I would have thought that I must use "void *" as the template parameter for pin_ptr but this seems not to work. The above code seems to work from initial tests.
-- Henrik
SDK1985
http://msdn2.microsoft.com/en-us/library/1dz8byfh
http://www.codeproject.com/managedcpp/pinnedpointers.asp
Pete0511
Thank you.
-- Henrik