1

I am currently calling the following com object using Powershell but am having trouble converting the code over to c#. I know this should be pretty straightforward and may be a dumb question, so I apologize if I look like a moron.

Powershell:

$nDeviceId=517
$wug = New-Object -ComObject CoreAsp.EventHelper
$wug.SendChangeEvent(2,$nDeviceId,1)

Current C# Attempt:

Type type = TypeDelegator.GetTypeFromProgID("CoreAsp.EventHelper");
Object application = Activator.CreateInstance(type);
pplication.GetType().InvokeMember("SendChangeEvent", BindingFlags.InvokeMethod, null, application, new object[]{2,517,1});

I appreciate any help!

Update: When I run the c# code I do not receive any errors.

vesuvious
  • 2,753
  • 4
  • 26
  • 39

3 Answers3

2

If you're using .NET 4 and don't mind late-binding (no intellisense, ...), you could use a dynamic object:

Type type = Type.GetTypeFromProgID("CoreAsp.EventHelper");
dynamic application = Activator.CreateInstance(type);

application.SendChangeEvent(...);
Joe
  • 122,218
  • 32
  • 205
  • 338
1

I'm not familiar with that COM-object, but in general COM-components are accessed by adding them as a reference in your c# project (select the com-tab in the add reference-dialog) and by using the classes they provide.

Best way to access COM objects from C#

Community
  • 1
  • 1
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • Yeah i tried to do this, but have not been able to locate the DLL that holds the COM object. I can see the object in the registry, but it does not give a file location. – vesuvious Feb 07 '14 at 18:58
0

Working off of Frode's answer, I found a DLL named CoreASP.dll and tried to import this into my project but received an error that the library was invalid.

I was able to use the "Type Library Importer" tool in visual studio to convert the COM type library into equivalent definitions.

command: tlbimp.exe CoreAsp.dll\1

This created another DLL "CoreAspiLib.dll" that I was able to import into my Visual Studio project.

Thank you Frode for leading me in the right direction.

Update:

So this method SOMETIMES works, it seems like I would have to call the command 3-4 times to get it to work as expected. To avoid the headache I ended up calling powershell from c#, making sure to hold the runspace open so that I don't have to reload it for subsequent commands (I may be calling this function 50+ times per minute).

Example of calling the script 1000 times:

Runspace runspace = RunspaceFactory.CreateRunspace();

string scriptText = 
       "$wug = New-Object -ComObject CoreAsp.EventHelper;" +
       "$wug.SendChangeEvent(2,519,1)";

runspace.Open();
for (int x = 0; x < 1000; x++)
    {
            Console.WriteLine("Running Event "+(x+1).ToString());
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(scriptText);
            pipeline.Commands.Add("Out-String");
            pipeline.Invoke();

            Thread.Sleep(1000);

     }

runspace.Close();

This seems to work 100% of the time.

Joe I also tried your method and ended up with the same result as my first test, no errors but the function did not run as expected.

vesuvious
  • 2,753
  • 4
  • 26
  • 39