Azurewrath

Hi all,

I am trying to do something that other python users are doing, but can't find how to do it in C#:

>>> obj = win32com.client.Dispatch("MAX.Application.9")
>>> obj._FlagAsMethod("loadmaxfile")
>>> obj.loadmaxfile("C:\\test.max")


Basically loadmaxfile is a function in the host app, which is registered like this:

registerOLEInterface #(loadmaxfile)

Shouldn't this be straightforward with C# too Either via Win32 or some other namespace




Thanks,
Aw


Re: Visual C# General C# COM

Citizen on the earth

Hi Axurewrath,

I can't understand you clearly and what are you going to do from your description.

Would you like to create a COM in C# If yes, try to check out this article about "Building COM Objects in C#" for reference - http://www.codeproject.com/csharp/com_object_in_c_.asp

Exposing .NET Framework Components to COM: http://msdn2.microsoft.com/en-us/library/zsfww439(vs.80).aspx

If you would like to know how to register a COM in C#, you can try to check out this MSDN document for details - http://msdn2.microsoft.com/en-us/library/w29wacsy(VS.80).aspx

If I have misunderstood you, please correct me and clarify what you are concerning. Thanks.

Regards,

Citizens on the earth





Re: Visual C# General C# COM

Mattias Sjogren

Looks like you're doing a late bound call to a method named loadmaxfile. You can do that with Type.InvokeMember in C#. Create the MAX.Application.9 object using Type.GetTypeFromProgID and Activator.CreateInstance.




Re: Visual C# General C# COM

Azurewrath

Thanks guys. Yes basically I am trying to use an application's scripting language's function, exposed using OLE. So python is able to call it, using the code I post above. But I don't know how in C#.

Looks like Mattias' reply talks about it, but I don't know how to find the information required by these functions, for the program




Thanks,
Aw




Re: Visual C# General C# COM

Mattias Sjogren

It's all there in your python code.

Code Snippet

Type t = Type.GetTypeFromProgID("MAX.Application.9");

object o = Activator.CreateInstance(t);

t.InvokeMember("loadmaxfile", BindingFlags.InvokeMethod, null, o, new object[] {"c:\\test.max"});






Re: Visual C# General C# COM

Azurewrath

Thanks so much man. But it didn't do anything but returns this in the output if VS:

A first chance exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe

I don't know how to make this work Sad




Thanks again,
Aw




Re: Visual C# General C# COM

Mattias Sjogren

Which line causes the error




Re: Visual C# General C# COM

Azurewrath

Hey man,

Now tried again, after registering the app as an OLE server, and it worked great. Sorry I know I should have done this before, just thought I might not register it correctly.

Thank you so much for this, it's super useful for me, and I have been wanting to do this since I have been using C#. You rock!




Thanks again,
Aw




Re: Visual C# General C# COM

Azurewrath

Also one last thing I wonder:

Can I also get data from the software, if I ask like how many light it has, etc. Is this method able to get what's been returned by the software




Thanks again,
Aw




Re: Visual C# General C# COM

Mattias Sjogren

Not sure I understand what you're asking. But InvokeMember will return whatever value loadmaxfile returned.






Re: Visual C# General C# COM

Azurewrath

Thanks man. It was what I meant. Yesterday I tried it (by casting it to the proper value) and it worked like a charm!

One thing that doesn't return is array. I believe this is because of the custom data type of the scripting language of the app I am using. I am wondering what should I do to ensure a proper conversion. Should I write something in C# that will recognize it and convert to array Or can I




Thanks alot again,
Aw




Re: Visual C# General C# COM

Mattias Sjogren

What is the type of the returned array on the managed side (ie what does returnValue.GetType().ToString() give you)






Re: Visual C# General C# COM

Azurewrath

Thanks alot man. I think it's always object, because otherwise the compiler wants me to cast the value to the type I declare like int.

But when I try to cast to int[] for instance it doesn't work.




Thanks alot again,
Aw