views:

78

answers:

2

What does this error mean in VB6?

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.

I keep getting it when i call a particular method of a dll that comes with windows xp and beyond (in system32 called upnp.dll)

Anyone any ideas? google wasn't helpful

+1  A: 

Well, the error message means that you're calling a function that can't be bound by VB6, possibly due to it have parameters or a return value of a data type that VB6 doesn't support. I sometimes worked around issues like this by writing a simple C++ COM object that called the function and "translated" it to be VB6 compatible.

You can sometimes also get this error message due to various typos, but I think you've already discovered that with your search on google so I'm assuming you've already checked for that.

If you post your code (or at least the name of the function that you're having problems with) it's possible that you could get a better answer.

ho1
What can I do on the vb6 end to remedy this and bypass the issue? I don't have access to editing the dll in question as it is provided by Microsoft. It should be usable in vb6 as it's ms written, can declaring it in an API declaration as we do for winapi stuff help? Since they are usually C friendly declaration wrappers... But I have no idea how to make that declaration even tho it might be simple as I'm not into C. Would you or anyone know?
Erx_VB.NExT.Coder
It is for .findbytype method in the upnpdevicefinder class inside upnp.dll implementation and usage of which is pretty easy. It's in windows/system32
Erx_VB.NExT.Coder
@Erx: Just because it's written by MS doesn't mean that it's usable from VB6. I don't know anything specific about that API, so all I can suggest is that you write a wrapper in C++ or possibly VB.Net or similar and then expose that as an automation compatible wrapper. But this of course depend on you either knowing C++ or being able to run a .Net wrapper.
ho1
Problem is target computers can't have .net installed and it just a vb6 app and I don't know c++ I also don't know what a wrapper would involve even if I did know the language, but I am happy to learn or do whatever I need to as long as I can run this in vb6 even if I need to write and use another dll to make it possible, I know Delphi, could that be useful? Any other options? R u sure I cant solve it in vb6 alone using a smart declaration or something?
Erx_VB.NExT.Coder
I know other people r using upnp.dll in vb6 without issue so I figured I must be doing something wrong or missed something simple but obvious as there are no flags raised re vb6 and upnp.dll in google
Erx_VB.NExT.Coder
@Erx: You could possibly have done it in Delphi (which I think supports unsigned types) but since Hans has already described a much easier solution below that's the way to do it.
ho1
ok thanks, but just to add, i noticed a ULONG type in VB6 which i used to declare a variable and set it to 0 and pass that variable to the function, didn't work, but i presume ULONG is an unsigned long? or?
Erx_VB.NExT.Coder
@Erx: Can't remember any ULONG type in VB6, so don't know.
ho1
thanks for your help mate - used my upvotes for today but i will upvote you as soon as they refill it in (apparently) 5 hours :)
Erx_VB.NExT.Coder
+5  A: 

This is the declaration for FindByType() as retrieved from the type library:

    HRESULT FindByType(
                    [in] BSTR bstrTypeURI, 
                    [in] unsigned long dwFlags, 
                    [out, retval] IUPnPDevices** pDevices);

Note the 2nd argument, unsigned long. VB6 doesn't support unsigned types. It is not a problem in VB.NET or C#, they do support them.

This problem is fixable if you have the Windows SDK installed. You should have it if you have a recent version of Visual Studio. Use the Visual Studio Command Prompt, then:

  • run oleview.exe c:\windows\system32\upnp.dll
  • type Ctrl+A, Ctrl+C to copy the type library content
  • run notepad.exe, Ctrl+V. Search for "unsigned" and delete it. There are two.
  • save the file to a temporary directory with the name upnp.idl
  • run midl upnp.idl /tlb upnp.tlb
  • copy the generated upnp.tlb to your project directory

You can now add upnp.tlb instead of upnp.dll, you should no longer get the error. -

Hans Passant
This is fantastic , I will try it as soon as I get back to computer, just a quick important question, will I need to package any of this modified info files or type library to the destination machines or will things just work on destination machines by including only upnp.dll and nothing else? Thanks
Erx_VB.NExT.Coder
You only need it to get your code compiled, it doesn't have to be deployed. Do check it in to source control.
Hans Passant
MarkJ
@Hans, i'm having a problem converting .idl to .tlb using midl - basically getting this error: "midl: command line error MIDL1005 : cannot find c preprocessor cl.exe" - the solutions to this problem involve changing default path settings but MSDN only describe this for VC++ installs and the dialogs/links do not exist for my VS2010 installation (VB.NET) and im wondering if i need VC++ for this or is there a place where i can change paths to add cl.exe to default paths?
Erx_VB.NExT.Coder
just to add, i've even tried doing the conversion of the idl to tlb inside the directory where cl.exe is, even that did not work *sigh* and have tried relative and absolute directories etc... nada... have looked at all command line options for midl, nothing usable worked. there must be a place where i can tell it where cl.exe is?
Erx_VB.NExT.Coder
@Erx: I did it for you, download from here: http://cid-dfa12d78ff70ce1b.office.live.com/self.aspx/.Public/upnp.tlb
Hans Passant
You can probably also use CallByName to invoke the method. That's what I do sometimes, though the TLB works better for other reasons too.
Bob Riemersma
@hans, thank you so much for your help, you've really made my week. i've learnt alot from your post. i have accepted answer and upvoted + upvoted some of your other posts ive been reading. thanks. i would love to know what i can do to have such a broad spectrum of knowledge like you do... to be as well armed like yourself? thanks again.
Erx_VB.NExT.Coder
@Erx: I appreciate the gesture, but please don't upvote other answers just because it is me. It isn't appropriate and there a script at SO that detects such voting patterns and cancels them. I'll lose them all, including your vote for this answer.
Hans Passant
gottcha, i'll be careful of that.... what can i do to know these obscure things about .NET, do you just read the entire framework specs or something or?
Erx_VB.NExT.Coder