|
pnpfriend wrote: ) does all the classes need Guid?
No. This is only needed if you are going to access the class from COM.
pnpfriend wrote: 2) Does all the class need interface classes? or can I have just regular class without interface and Guid?
No, classes are not required to implement an interface. Interface essentially allow you to define the "contract" that the class will provide. It does nothing to ensure how that contract is implemented but only that specific methods/properties will be available.
pnpfriend wrote: Can library have static function/procedures so other classes can call them directly?
The library can't, but an individual class can have static methods and/or properties that can be called without requiring an instance of that class.
pnpfriend wrote: Can I have a class, that will not be calling from COM client, without a guid?
This is pretty much the same as question 1, just asked inversely...so yes, you can have a class that isn't called from COM without a GUID.
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|
|
I am trying to work out a simple COM/.NET interop code. In my project in VB, I have created a simple windows application to add two numbers. The functionality of adding is done through the help of a .NET DLL written in C#. I am quite confused by trying various ideas sugggested by people. Can anyone clarify this simply in a step by step fashion?
This my VB code:
Private Declare Function method Lib "C:\Documents and Settings\CSfiles\interop\interop\bin\Debug\interop.dll" Alias "interopdll" (ByVal X As Integer, ByVal X As Integer)
Private Sub Command1_Click()
Dim a As Integer
Dim obj As Object
obj = New sample ' I get a compile-time error for undefined user type here.
a = obj.method(CInt(Text1.Text), CInt(Text2.Text))
Label1.Caption = CStr(a)
End Sub
My C#(.NET DLL) Code:
using System;
using System.Runtime.InteropServices;
using System.Reflection;
[assembly: ComVisible(true)]
namespace interop
{
/// <summary>
/// Summary description for Class1.
/// </summary>
[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("6CE9C732-CD90-4042-A5F0-CF71DFAC2598")]
class sample
{
public sample()
{
}
public int c;
public void method(int a, int b)
{
c = a+b;
//return c;
}
}
}
In this I have registerd the DLL with /codebase switch while using regasm.exe too.
Regards,
Lenus.
modified on Monday, March 24, 2008 3:49 AM
|
|
|
|
|
Statement "Private Declare Function method Lib..." is unwanted and should be removed.
I.1. Add typelib into VB project by menu "Project" -> "References...". Search your DLL or TLB there (something like "interop") and select it.
I.2. Create new instance with New and Set operator
Dim obj As sample
Set obj = New sample
II. Create new instance with CreateObject and Set operator
Dim obj As Object
Set obj = CreateObject("interop.sample")
With best wishes,
Vita
|
|
|
|
|
Hi All,
I have generated a tlb file through idl for the implementation of COM.
I want to use this COM object in my VC++ code.
Anybody know how to reference tlb file in VC++ code.
For VB I can directly reference the tlb file, but I dont dont know is there any way we can reference tlb file in my VC++ code and start using the methods inside COM object
Thanks in Advance
JK
|
|
|
|
|
Use #import[^] to import TypeLibrary(.TLB file)
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Thanks Sohail,
Thanks for the good article.
I will implement and see.
Thanks
JK
|
|
|
|
|
OK. Let me know if you have further problems with this.
Cheers,
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|
|
You have to import the tlb file to ur VC++
I declare it in header file.
#import "C:\myDll\mydll.tlb" raw_interface_only
...
..
public Void INiComObj()
{
CoInitialize(NULL);
HRESULT hr = CocreateInstance(CLSID...)
}
|
|
|
|
|
Hi Sohail,
Thanks for the good article.
And thanks to pnp for giving the sample code.
I did import my tlb file with raw_interface_only option.
#import "C:\temp\Mytlb.tlb" raw_interface_only
I am facing following compilation error in *.tlh file.
error C2504: 'AnyObject' : base class undefined
When I see my tlh file my class is deriving from AnyObject base class.
I am not sure why it is deriving from AnyObject?
Is there any base class "AnyObject" in C++? If yes what are the include files i need to include? Please help me.
Thanks in advance.
Regards
JK
|
|
|
|
|
Hi All,
I found out the solution for error C2504: 'AnyObject' : base class undefined
My tlb is dependent on some other tlb so, what i did is I #import the base tlb before importing my tlb. It complies fine. There are no errors.
But, I am not able to get pointer to my COM object, when I am running following sample code
CoInitialize(NULL);
IMyComObject pObject(_uuidof(MyComObjectName));
I am getting following error in CoCreateInstance method
0x80040154 Class Not Registered
I checked the registry, there is an entry for my typelib.
I am not sure why it is not able find the class.
Any suggestions/help please..
Thanks in advance
JK
|
|
|
|
|
Jahnson K wrote: I am getting following error in CoCreateInstance method
0x80040154 Class Not Registered
The TLB describes the all data of COM server, but there is no code in TLB. You should also register COM server, i.e. EXE or DLL executable module.
With best wishes,
Vita
|
|
|
|
|
Thank you all,
I am able to get the COM object.
There was couple of mistates I was doing while creating CoCreateInstance.
Now it works fine.
Thanks
JK
|
|
|
|
|
there are two ways
1)Use #import <typelibrary.tlb>
second you can use the class wizard and refer the tlb file, the wizard will generate the .cli and .clh file containing the wrapper around your COM component
Regards,
Sunil Tonger
|
|
|
|
|
Hello friends,
I am working on CRM application that uses the 3rd party TAPI OCX(topTAPI).
All the features related to call control like call answer,call end,call
hold,call unhold works good.But the transfer and conferencing functions
of this ocx doesn't working.If anyone of you guys had used topTAPI ocx
earlier then kindly tell me the problem.I've read its documentation
properly and it doesn't allow me to initialize any of the conferencing
related functions.
OR
If anyone has the solution apart from topTAPI please let me know.
The sample code would be appreciated.
Thanks in Advance.
ritz1234
|
|
|
|
|
Hi all
I'm calling IPersistFile:Save on my local machine logged in with administrator rights and I'm getting a security error any ideas would be very welcome.
I'm using VS2005 on XP SP2
Here's the code, it's basically pulled out of the VS documentation:
HRESULT CShellLink::CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc, LPCSTR lpszArgs)
{
HRESULT hres;
IShellLink* psl;
::CoInitialize(NULL);
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);
psl->SetArguments(lpszArgs);
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
if ( ! MultiByteToWideChar(CP_ACP, 0, lpszPathLink, -1, wsz, MAX_PATH)) {
DisplayLastError();
} else {
hres = ppf->Save(wsz, TRUE);
if ( ! SUCCEEDED(hres)) {
DisplayLastError();
}
}
ppf->Release();
}
psl->Release();
}
return hres;
}
Cheers
Tom
Philosophy: The art of never getting beyond the concept of life.
Religion: Morality taking credit for the work of luck.
|
|
|
|
|
TClarke wrote: I'm calling IPersistFile:Save on my local machine logged in with administrator rights and I'm getting a security error any ideas would be very welcome.
I'm using VS2005 on XP SP2
Since it is a security error, I think it is important to know where you are saving to. Even as an administrator, there are parts of the system you aren't allowed to modify.
Nathan
|
|
|
|
|
I'm getting the same error on W7 using VS 2008 and the same sample code. I am writing to a temp folder with security set to all access for "Everyone" from a shell running with Administrator priviledge. It still doesn't work.
Probably, as usual, MS sample code is incomplete, or not even wrong.
-Geoff
|
|
|
|
|
Hello all, I know the experts on codeproject can deal with any of my sneaky questions... try this one:
What .tlb is the definition of IDataObject in?
Why do I need it: I have my own interface with IDataObject as an argument of one of its methods. The type library compiled from that .idl contains beside my own interface also IDataObject and a couple of others, which are referenced by it. As a consequence, the proxy for that interface is wrong and drag and drop stops functioning in my app . I tried to find a type library to put in importlib("") statement, but did not find it. There must be something obvious I am overlooking.
Thanks for any advice
|
|
|
|
|
Vlasta_ wrote: Why do I need it: I have my own interface with IDataObject as an argument of one of its methods. The type library compiled from that .idl contains beside my own interface also IDataObject and a couple of others, which are referenced by it. As a consequence, the proxy for that interface is wrong and drag and drop stops functioning in my app . I tried to find a type library to put in importlib("") statement, but did not find it. There must be something obvious I am overlooking.
I can think of a couple other options. One is to provide custom marshaling for your method. Another is to just pass an IUnknown and query for IDataObject in your code. I tried to find the tlb for IDataObject and I couldn't find it either.
Nathan
|
|
|
|
|
Thanks, I may end with the IUnknown workaround, but I am still hoping to locate the tlb somwhere.
|
|
|
|
|
Look at ...\Microsoft Visual Studio\Common\IDE\IDE98\VJEXT.TLB
With best wishes,
Vita
|
|
|
|
|
I've a component(dll) has developed in vb. I can able to refer the dll and the method can be called in vb. I've checked its working fine. But in asp I've created object using Server.CreateObject and when I call the same method, it shows "permission denied" error. If anybody having idea pls. help me.
Rishihar S
WinCrs
|
|
|
|
|
|
Hi,
I have one MDI application TrialApp.exe and a COM exe MathOperation.exe.COM exe was generated through ATL.Now from a modeless dialog in TrialApp.exe i am accessing a model dialog in MathOperation.exe.
1.Run TrialApp.exe
2.Modeless dialog pops up.
3.Enter the input values.
4.Press a button->calculate.
5.Pops up a dialog from MathOperation.exe
6.Now without closing the dialog(from MathOperation.exe) just click on the modeless dialog(TrialApp.exe)
7.Shows server busy message.
This happens in XP and Vista.If i make the dialog model in TrialApp.exe then the message is not coming.Please help.
Thankyou
modified on Wednesday, March 19, 2008 1:30 AM
|
|
|
|
|
I may not be correct here because I don't know how your COM server is implemented (design/architecture)
I believe your TrialApp.exe dialog is making a blocking call to MathOperation.exe (specifically it's waiting for MathOperation.exe method to return, but this method has an active Modal dialog).
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|