Advertisement

COM Interop and Accessing the Win32 API

  • Dan Appleman

Abstract

Once upon a time there was a programmer. This programmer had written many programs over the years and they worked rather well. One day, the spirit of the great OS appeared before him and presented a brand new way of programming—one that would allow the programmer to write many more programs that would work even better than before.

Keywords

Visual Studio Declare Statement Late Binding Class Interface Visual Basic Programmer 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Reference

  1. 1.
    With which I have no connection whatsoever.Google Scholar
  2. 2.
    Code that is not managed by the.NET Framework.Google Scholar
  3. 3.
    Unfortunately, a tutorial of COM is beyond the scope of this book. If you are unfamiliar with COM, you can still follow the discussion just ignore COM-based terms such as automation and IDispatch that you don’t understand. If you really want to learn about COM from a VB perspective, consider my book DevelopingCOM/ActiveX Components with Visual Basic 6: A Guide to the Perplexed (Que, 1998).Google Scholar
  4. 4.
    This discussion will focus on local assemblies only. If you wish to create an RCW assembly to be deployed in the global assembly cache, use the TLBImp utility described in the framework documentation to create a signed RCW. You’ll read more about deployment issues in Chapter 16.Google Scholar
  5. 5.
    The thread number will, of course, vary. The line “From other thread” may appear at different locations in the results depending on the interaction between threads.Google Scholar
  6. 6.
    I was burned several times using public structures in VB6 and while it’s possible they solved the problems in later service packs, I never felt the need to go back to using them.Google Scholar
  7. 7.
    To be honest, I haven’t heard much about how well.NET hosts VB6-authored ActiveX controls. Keep in mind that ActiveX controls are much more complex than ActiveX DLL and ActiveX EXE components and are more likely to exhibit subtle differences in behavior in the.NET environment. Microsoft has invested a huge effort in interop but if there are imperfections, ActiveX controls are where they are most likely to appear.Google Scholar
  8. 8.
    Actually, more, but those perform standard COM tasks such as handling events and do not implement the methods you define.Google Scholar
  9. 9.
    I will not go into typelib versioning issues at this time—it’s unimportant for our purposes here.Google Scholar
  10. 10.
    I have never seen an exception to this, even though it is undocumented as far as I know.Google Scholar
  11. 11.
    With VB6, adding members to an interface was inadvisable and violated the COM contract, but it did work.Google Scholar
  12. 12.
    And when I figure out what those plans actually are.Google Scholar
  13. 13.
    You can export functions from VB.NET or C# assemblies or VB6 DLL’s using Desaware’s SpyWorks.Google Scholar
  14. 14.
    For those who are curious, the VB.NET declare statement compiles into the dllimport attribute. So even though the syntax is very different from C#, the resulting IL code, and performance, is identical to that of C#.Google Scholar
  15. 15.
    They still contain 16-bit code, believe it or not.Google Scholar
  16. 16.
    You can access the Unicode entry point when using VB6 if you are willing to handle the string access yourself. My book Dan Appleman’s Win32 API Puzzle Book and Tutorial for Visual Basic Programmers (Apress, 1999) covers this subject in nearly excruciating depth.Google Scholar
  17. 17.
    For one thing, I don’t know how to handle every one of those functions—oh, I’m sure I can figure them all out but the fact is, I don’t have code demonstrating using P-Invoke to call nine thousand plus functions and I doubt anyone else does either.Google Scholar
  18. 18.
    If not, I occasionally do this kind of thing on a consulting basis—but it ain’t cheap.Google Scholar
  19. 19.
    If you want to learn more about using C++ in this way, check out Jonathan Morrison’s book C++ for VB Programmers (Apress, 2000). It’s a good way for VB6 programmers who know no C++ to start out with the language.Google Scholar
  20. 20.
    The Unmanaged Code Debugging checkbox on the Debugging page of the VBlnterface project property pages must be checked for this to work.Google Scholar
  21. 21.
    The Marshal.PtrToStringAnsi, Marshal.PtrToStringAuto, and Marshal.PtrToStringUni methods allow you to obtain a.NET String given a pointer to the unmanaged heap. They work similarly to the Marshal.PtrToStructure method that you’ll see later in this chapter.Google Scholar
  22. 22.
    Desaware’s SpyWorks includes a user-defined type byte-packing and unpacking component to help in cases like this.Google Scholar
  23. 23.
    Windows 2000 additions to the structure have been left out to reduce complexity. Windows determines which version of the structure you are using based on the dwSize field.Google Scholar
  24. 24.
    You might be wondering why I didn’t try to marshal the entire array of RASENTRYNAME structures at once. The answer is simple: I couldn’t find a way to make it work. Marshaling the array data to the API function was no problem but I had no success marshaling the data back. The P-Invoke system knows how to handle arrays of simple types but the documentation does not state how it handles arrays of structures and at this time, they do not seem to be marshaled back. Whether this is normal behavior or a bug is not clear. Watch the Web site for updates.Google Scholar
  25. 25.
    Again, we are not using the latest version of the structure with Windows 2000 additions.Google Scholar

Copyright information

© Daniel Appleman 2003

Authors and Affiliations

  • Dan Appleman

There are no affiliations available

Personalised recommendations