473,396 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get IDispatch from DLL?

I've got an out-of-process COM server (EXE) that I want to be able to
control from Visual Basic. I can already control it from a C/C++ app
without any issues. Under VBA in MS Access 2000, which I think is
similar to VB6, I'm able to get things to work, but now under VB 2005
(Visual Studio 2005) I can't get the same to work. Here is what I'm
doing:

1. Because the COM server only registers with the running object
table, but doesn't create class ID and prog ID registry settings (or
anything in the registry) I can use the normal GetObject function to
get the running object because VB wants to translate the class ID by
looking in the registry, and my COM server doesn't even have a class
ID. I just registers with the running object table when it is
running, using an item moniker.

2. So to get around this fact I wrote a simple DLL in C to get the
object from the running object table and return a pointer to the
object. The function is delcared as returning IDispatch* (pointer to
IDispatch).

3. Under MS Access 2000 VBA I'm able to call the DLL like this:

dim app as object
set app = GetApp()

Then I can call whatever IDispatch functions I want on "app". All
works well.

Under VB 2005, I get an error at the GetApp() line saying that a
function cannot return a variant. I don't have the exact error right
now, because I'm actually posting this for a 3rd party, but I'll try
to get it if needed.

Am I going about this the wrong way, is there an easier way, maybe one
without using a DLL? I can't change the way the COM server (EXE)
registers itself, or very much else about the server.

Apr 26 '07 #1
4 3896
>Under VB 2005, I get an error at the GetApp() line saying that a
>function cannot return a variant. I don't have the exact error right
now, because I'm actually posting this for a 3rd party, but I'll try
to get it if needed.
Please post the exact error when you can, along with the signature of
the GetApp function.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 27 '07 #2
On Apr 26, 10:20 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
eselk wrote:
Under VB 2005, I get an error at the GetApp() line saying that a
function cannot return a variant. I don't have the exact error right
now, because I'm actually posting this for a 3rd party, but I'll try
to get it if needed.

Please post the exact error when you can, along with the signature of
the GetApp function.

Mattias
>From my C++ code:
extern "C" IDispatch* __export PASCAL GetApp(char *name);

The test code that works under MS Access 2000:

Private Declare Function GetApp Lib "c:\mydll.dll" (ByVal name As
String) As Object

Private Sub Command0_Click()
username = "bob"
Dim app As Object
Set app = GetApp(username)
If app Is Nothing Then
MsgBox "App not running, or not running as " & username
Exit Sub
End If
Dim msg As Object
Set msg = app.NewMessage()
Set app = Nothing
If msg Is Nothing Then
MsgBox "NewMessage failed!"
Exit Sub
End If
Dim t As Byte
t = 69
msg.Type = t
msg.To = "the to field"
msg.From = "the from field"
msg.Body = "the body" & Chr$(13) & "line 2 of the body"
msg.Subject = "the subject"
msg.Display
Set msg = Nothing
End Sub

--------
The same code which a 3rd party has said they cut and pasted into VB
for Visual Studio 2005, is giving them this error:

System.Runtime.InteropServices.MarshalDirectiveExc eption was unhandled
Message="PInvoke restriction: cannot return variants."

at this line:

Set app = GetApp(username)

------

I guess Object is a variant in VB 2005? But in VB6 (aka VBA 2000)
Object is an IDispatch? Is there another type that I should use for
IDispatch under VB 2005? Or do I simply need to change the GetApp()
function so it passes the Object as an argument instead of a return
value? If so, what is the correct decleration on both sides to do
that?

Thanks for the time!

Apr 27 '07 #3
>I guess Object is a variant in VB 2005?
That's the default interpretation, yes. But you can change it with an
attribute.

Private Declare Function GetApp Lib "c:\mydll.dll" (ByVal name As
String) As <MarshalAs(UnmanagedType.IDispatch)Object
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 28 '07 #4
On Apr 28, 3:24 am, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
I guess Object is a variant in VB 2005?

That's the default interpretation, yes. But you can change it with an
attribute.

Private Declare Function GetApp Lib "c:\mydll.dll" (ByVal name As
String) As <MarshalAs(UnmanagedType.IDispatch)Object

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Thank you, that worked. The 3rd party says they aren't able to get
early binding to work, but I'll let them deal with that, since they
said they are at least able to make it work with late binding. I
think they probably need to declare the object as an IDispatch now,
somehow, instead of just "Object", and then call Invoke() directly,
instead of letting "Object" call it indirectly.

Apr 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Sparago | last post by:
I am working with the MSHTML editor control. I used win32com.client.gencache.EnsureModule on the MSHTML type library to generate the necessary IDispatch support for all the various interfaces used...
3
by: Roger That | last post by:
Hi, I am trying to use the function "CreateStreamOnHGlobal" from python code (I need to pass an IStream object to MSXML, like I do in C++ code). I was able to retrieve a pointer on the IStream...
0
by: | last post by:
well, how can i convert these code to VB.NET? thx CallJScript(const CString strFunc, const CStringArray& paramArray) { CComPtr<IHTMLDocument2> m_spDoc; m_spDoc.Attach(...
5
by: Dominic Godin | last post by:
Hi, I have decleared the IDispatch interface but I'm having trouble using it. Word.Application MSWord = new Word.ApplicationClass(); IDispatch wb = (IDispatch)MSWord.WordBasic; int lcid =...
1
by: Dmitry Medvedev | last post by:
Hello, I need to write C# class, that would implement IDispatch interface to be used in ActiveScript. I can't use IReflect interface here due to design issues, I need to handle GetIDsOfNames...
4
by: Phil Coveney | last post by:
Hello, I am implementing a class in C# that wraps an automation server and am stuck on something basic. Using the following code fragment: using System; using System.Runtime.InteropServices;...
8
by: Charles Law | last post by:
Hi guys There's nothing much going on in Interop at the moment, so I'll post this here as well in case someone can give me a quick answer. The following is an extract from MSDN: <extract>...
0
by: Jim Hubbard | last post by:
How would I implement the IDispatch interface to handle the following in VB.Net <BEGIN> Controlling Download and Execution The WebBrowser Control gives you control over what it downloads,...
3
by: Huayang Xia | last post by:
I am trying to use ctypes to call dll functions. One of the functions requires argument "struct IDispatch* ". I do have a PyIDispatch object in python. How can I convert this "PyIDispatch object"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.