COM Interop, DllImport and Add Link

I'm trying to get into COM interoperability

So there is a simple example:

SpeechLib.SpVoice voice = new SpVoice(); voice.Speak("Hello sucker!",SpeechVoiceSpeakFlags.SVSFDefault); 

Of course, I have to add the link to% windir% \ system32 \ speech \ common \ sapi.dll before, and VS will add Interop.SpeechLib.dll to the project folder, and now I need to distribute this 200kb library using my simple 4kb application.

Is it possible to use [DllImport] instead of adding a link, because in most cases the speech library is already present on the client computer?

Could you show me how to rewrite the code above using the DllImport method?

+4
source share
4 answers

You are unlikely to be able to use [DllImport] instead of "Add Link" if the Speech API is a COM API.

[DLLImport] is used to call Win32 unmanaged DLL files, while Add Link is shortcut for running tlbimp.exe (typelib import) to enable .NET interaction with COM.

You can learn more about COM Interop here and DllImportAttribute here . Pinvoke.net is a great site for finding DllImport signatures.

+5
source

[DllImport] is not what you want. However, the interop library that you see when you add a reference to COM is just a convenience; You can always write your own.

The easiest way to see this is to use Reflector to parse the COM interaction assembly (Interop.SpeechLib.dll in your case) into the source tree.

The interop assembly will contain .NET declarations for all COM types in the typelibrary that you usually want, but if you only need a small subset, you can get rid of bits that you don't need.

If you don't like having a second DLL dependency, you can even add the source files created by Reflector to your existing project, thereby compiling them into your assembly.

+2
source

If I understand, you are trying to make the last binding. I suggest you use Reflection. Check out this post: http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx

Hope this helps

0
source

To reduce the file size, you can get the Copy Local link to false .

0
source

All Articles