Click to See Complete Forum and Search --> : System::Runtime::InteropServices Out parameter


cowabunga
February 2nd, 2009, 03:37 AM
Hi,

I have a static lib - and I have created a wrapper dll for exporting some functions from the static lib. Now, I am trying to access the exported functions from Managed C++ on Visual Studio 2005. I have an "out" parameter and I am using Interop services. But I get a compiler error saying
error C2039: 'Out' : is not a member of 'System::Runtime::InteropServices'

Giving my code snippet below:

#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

public ref class LibWrap
{
public:
[DllImport("demo.dll", SetLastError=true,
CallingConvention=CallingConvention::StdCall)]
static void Create([System::Runtime::InteropServices::Out] IntPtr handle);

[DllImport("demo.dll", SetLastError=true,
CallingConvention=CallingConvention::StdCall)]
static void Initialize(IntPtr hIpoptHandle, String^ paramsFile);
};


int main(array<System::String ^> ^args)
{
IntPtr handle;

LibWrap::Create([System::Runtime::InteropServices::Out] handle);
LibWrap::Initialize(handle, "abc");
Console::WriteLine("Hello World");
}

On compilation I get these errors,

error C2143: syntax error : missing ')' before '['
error C2660: 'LibWrap::IpOptCreate' : function does not take 0 arguments
error C2039: 'Out' : is not a member of 'System::Runtime::InteropServices'
error C2065: 'Out' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'handle'
error C2059: syntax error : ')'


Any pointers on the same would be appreciated !

Thanks!

darwen
February 2nd, 2009, 05:24 AM
1. Please use code tags. See the link in my post.
2. You shouldn't have the attribute in the call to Create.

i.e.


LibWrap::Create(handle);


3. You're passing 'IntPtr handle' by value : it's not an output parameter and the function cannot return anything. What's the native function declaration of Create ? If you're passing a reference to a pointer or a pointer to a pointer the declaration of create should be :


[DllImport("demo.dll", SetLastError=true,
CallingConvention=CallingConvention::StdCall)]
static void Create([System::Runtime::InteropServices::Out] IntPtr %handle);


'%' is the managed version of '&' in native C++.

4. You have a Create but no Destroy or the like : C++/CLI needs to be able to destroy the memory and resources of the pointer that you've created in your dll and passed out to it. It can only do this by supplying a function in the dll which destroys the pointer it's passed which has been created by Create.

Darwen.

cowabunga
February 2nd, 2009, 07:37 AM
Hi Darwen,

Thanks for your answers!

3. The wrapper dll has the following

#define PLSHANDLE32(name) struct name##__ { int unused; }; \
typedef const struct name##__ * name;

PLSHANDLE32(HIPOPTHANDLE);

UnManagedWrapper *HToPtrDialogFilters(HIPOPTHANDLE h) {assert(h); return (UnManagedWrapper* )h;};

HIPOPTHANDLE PtrToHIPOPTHANDLE(UnManagedWrapper *p) {assert(p); return (HIPOPTHANDLE )p;};

void __declspec( dllexport ) Create(HIPOPTHANDLE *hIpoptHandle)
{
UnManagedWrapper* pFilters = new UnManagedWrapper(false, false);
*hIpoptHandle = PtrToHIPOPTHANDLE(pFilters);
}

How should my DllImport function look like in this case?

4. Well, I do have a Destroy method - but I didn't post the whole code. I just posted a snippet.

Nandini

Alex F
February 2nd, 2009, 09:07 AM
Why do you use PInvoke in C++/CLI? You can call unmanaged functions directly, like from native C++ code. This is called IJW (It Just Works), and this is the main advantage of C++/CLI.

cowabunga
February 2nd, 2009, 09:50 AM
Alex,

I ultimately have to interface the dll wrapper from C#. Am not too familiar with C# - hence was trying to imitate the scenario from managed C++.

Thanks!