.NET server COM client interop problem (I think?)

V

Victoria Penrice

Dear all,

I've written a dll (in C#), to be used as a COM object in VB6. Say (for
simplicity) I've defined two classes in that dll: ClassFunc and
ClassArgs. One of the functions in ClassFunc (say, called
"FunctionToCall") takes an instance of ClassArgs as a parameter, like
this:

public void FunctionToCall( ClassArgs arg )

Now, imagine we are in VB6. We've created instances of both classes, and
successfully initialised them. But as soon as we try to use
FunctionToCall, we get an error. If we redefine FunctionToCall and make
it take a string (for example), everything works fine.


To expand, here's how I defined the first class:

public class ClassArgs
{
public ClassArgs(){}
private string m_FileName = "";

public void SetFileName( string FileName )
{
m_FileName = FileName;
}
}

...then the second class:

[Guid("13A803AF-CA7D-46b6-8485-9C3D1A549B09")]
public interface ClassFuncInterface
{
void FunctionToCall( ClassArgs ags );
}
//... some code for events omitted here

[Guid("4749F529-744E-4642-AF71-BDA6C8068D8B")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class ClassFunc: ClassFuncInterface
{
public ClassFunc(){}
private string FILE_NAME = "ComTestOk.txt";

public virtual void FunctionToCall( ClassArgs args )
{
StreamWriter sr = File.CreateText(args.GetFileName());
sr.Close();
}
//... some code for events omitted here
}

//********************************************************

I register my dll with COM and provide a ref to the corresponding tlb in
my VB6 app, where I use my classes as follows:

Dim WithEvents Func As ClassFunc
Private Sub Command1_Click()
Set Func = New ClassFunc
Dim Args As ClassArgs: Set Args = New ClassArgs
Args.SetFileName ("SomeFile.txt")
Func.FunctionToCall (Args)
End Sub

//********************************************************

I am getting an "Invalid procedure call or argument" error at line:

Func.FunctionToCall (Args)


Any ideas, please?

Thank you.
 
N

Nicholas Paldino [.NET/C# MVP]

Victoria,

Since ClassArgs needs to be used by the call to FunctionToCall, you
should export that correctly in COM like you would any other class. You
should change the method definition to take a type of IClassArgs, and define
IClassArgs as a separate interface, and pass around instances of that.

Hope this helps.
 
V

Victoria Penrice

Thank you for your reply, Nicholas, I have now redefined my ClassArgs as
follows:

[Guid("13A803AF-CA7D-46b6-8485-9C3D1A549B00")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ClassArgsInterface
{
void SetFileName( string FileName );
}
[ClassInterface(ClassInterfaceType.None)]
public class ClassArgs : ClassArgsInterface
{
public ClassArgs(){}
public string m_FileName = "";

public void SetFileName( string FileName )
{
m_FileName = FileName;
}
}


...and redefined my FunctionToCall in ClassFunc as:


void FunctionToCall( ref ClassArgsInterface args )
{
//...
}

But now, how do we approach the problem of passing of the interface to
FunctionToCall()? How do we get a "pointer" to ClassArgsInterface in
VB6, please?

Thank you.
 
N

Nicholas Paldino [.NET/C# MVP]

Victoria,

Why are you passing it by ref? You don't need the ref.
 
V

Victoria Penrice

I got rid of "ref" in my C# code... and I also got rid of "As ClassArgs"
in:

Dim args As ClassArgs: Set args = New ClassArgs

...so now it looks like

Dim args
Set args = new ClassArgs

...and it works a treat! =)

Thanks to all for your help!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top