Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to convert a Powershell script to C#.

The Powershell script accesses the Windows Deployment Service COM Object:

$wdsObject = New-Object -ComObject WdsMgmt.WdsManager
$wdsServer = $wdsObject.getWdsServer("Localhost")
[...]

Is it possible to access these COM Objects in C#?

I couldn't find the corresponding COM Object reference in Visual C#. I tried to add the wdsmgmt.dll from System32 as a reference in a C# project, but that didn't work. I'm quite stumped otherwise.

EDIT:

Answer to comment 1: Best way to access COM objects from C#

The COM Object in question isn't listed in the references. I've looked through the list about 5 times to be sure I wouldn't miss it. Would there be a way to make that COM Object show up in the COM references list in C#?

Answer to comment 2 (what did not work): Trying to add wdsmgmt.dll shows the following error:

Please make sure the file is accessible, and that it is a valid assembly or COM component.

The file is accessible, but it doesn't seem to be a valid COM component in the eyes of C#.

A search in the registry for WdsMgmt shows that the COM Object is at least somehow present:

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{CD583E47-B079-4025-A799-5F951D016D3E}

Maybe the rephrased question would be:

How do I add a COM Object reference to C# when I know it's name in Powershell?

share|improve this question
    
See if this helps you stackoverflow.com/questions/635839/… –  Frode F. Jan 30 '13 at 9:19
1  
"that didn't work": Ok, what exactly have you tried, and in what way did it not work? –  Jean Hominal Jan 30 '13 at 9:21
1  
Thank you for your comments, I have edited my question to reflect on your inputs. –  Khôi Jan 30 '13 at 9:40
    
I think wdsmgmt.dll is an MMC snap-in. It may well be that you can only access this functionality via C++. This might help: msdn.microsoft.com/en-us/library/bb530729(v=VS.85).aspx (all the code samples are in C++) –  5arx Jan 30 '13 at 9:48

2 Answers 2

up vote 1 down vote accepted

You can directly mirror what PowerShell does by using dynamic, and creating the instance by ProgId. See http://stackoverflow.com/a/3251325/8446 for example.

share|improve this answer
    
This was the hint that I needed to get this done! The application only compiled in x64 architecture though. –  Khôi Feb 2 '13 at 15:03

Have you tried using TlbImp?

Creating a COM Class Wrapper

For C# code to reference COM objects and interfaces, you need to include a .NET Framework definition for the COM interfaces in your C# build. The easiest way to do this is to use TlbImp.exe (Type Library Importer), a command-line tool included in the .NET Framework SDK. TlbImp converts a COM type library into .NET Framework metadata — effectively creating a managed wrapper that can be called from any managed language. .NET Framework metadata created with TlbImp can be included in a C# build via the /R compiler option. If you are using the Visual Studio development environment, you only need to add a reference to the COM type library and the conversion is done for you automatically.

Here is more information about this interop tool:

Tlbimp.exe (Type Library Importer)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.