ref or out parameter in delegate, declared in C++

  • Thread starter Thread starter Eugene Mayevski
  • Start date Start date
E

Eugene Mayevski

Hello,

I am trying to declare in C++ and use in C# a delegate which contains
"ref" and "out" parameters.

I declare a delegate as:

public __delegate void SolFSCreateFileEvent(SolFSStorage* Sender,
System::String* FileName, Handle &File, bool Overwrite, bool
IsJournalFile, Error &Result);

(I also tried "[Out] Error* Result" and Error* Result, no luck).

In C# I declare a method:

public void OnCreateFile(SolFSStorage Sender, String FileName, ref UInt32
File, bool Overwrite, bool IsJournalFile, ref int Result)

and try to create an instance as

new SolFSCreateFileEvent(OnCreateFile)

What I get is compiler error:

Method 'SolFSExplorer.MainForm.OnCreateFile(SolFS.SolFSStorage, string,
ref uint, bool, bool, ref int)' does not match delegate 'void
SolFS.SolFSCreateFileEvent(SolFS.SolFSStorage, string, uint*, bool,
bool, int*)'


If I change C# declaration to

public void OnCreateFile(SolFSStorage Sender, String FileName, UInt32
*File, bool Overwrite, bool IsJournalFile, int *Result)

then the previous error disappears but another one appears, saying that
* is only allowed in unsafe context (sounds reasonable <g>).

What can be a solution besides declaring CreateFileEventArgs helper
class and passing it as a single parameter?

Free copy of SolFS (http://www.eldos.org/solfs/solfs.html) to be given
to first person who knows a solution.

Sincerely yours,
Eugene Mayevski
 
Eugene,

The solution is to declare the delegate as follows:

public __delegate void SolFSCreateFileEvent(
SolFSStorage* Sender,
System::String* FileName,
System::UInt32 __gc * File,
bool Overwrite,
bool IsJournalFile,
System::Int32 __gc * Result);

A C# ref parameter corresponds to a C++ managed pointer to a value type.

Have I won? :-)

Greetings

Bart Jacobs
 
Bart Jacobs wrote:

BJ> public __delegate void SolFSCreateFileEvent(
BJ> SolFSStorage* Sender,
BJ> System::String* FileName,
BJ> System::UInt32 __gc * File,
BJ> bool Overwrite,
BJ> bool IsJournalFile,
BJ> System::Int32 __gc * Result);

public __delegate void SolFSCreateFileEvent(SolFSStorage* Sender,
System::String* FileName, System::Int32 __gc * File, bool Overwrite,
bool IsJournalFile, System::Int32 __gc * Result);

Looks like the same, right (except File parameter type, but this doesn't
matter)? Here's what I get from compiler:

c:\Projects\SolFS\DotNet\DotNet.h(35): error C2144: syntax error : 'int'
should be preceded by ','
c:\Projects\SolFS\DotNet\DotNet.h(35): error C2501: 'Result' : missing
storage-class or type specifiers
c:\Projects\SolFS\DotNet\DotNet.h(36): fatal error C1903: unable to
recover from previous error(s); stopping compilation

(correction from e-mail) caret is on the first position of the line, not
indicating the exact error, but removing the last parameter (Result)
doesn't help.

This is about the above line. Any ideas? If I remove __gc, the error is the
same. So this must be something with System::Int32? But why is
System::String ok then?

Sincerely yours,
Eugene Mayevski
 
Eugene said:
public __delegate void SolFSCreateFileEvent(SolFSStorage* Sender,
System::String* FileName, System::Int32 __gc * File, bool Overwrite,
bool IsJournalFile, System::Int32 __gc * Result);

Must be public __delegate void SolFSCreateFileEvent(SolFSStorage*
Sender, System::String* FileName, UInt32 __gc &FileHandle, bool
Overwrite, bool IsJournalFile, Int32 __gc &Result);

But as your tip really helped, please contact me for the license and
give me the name and e-mail for registration record.
 
Back
Top