|
|
Comments and Discussions
|
|
 |

|
I generally don't like stepping on people's toes, but I think your code example is incorrect and incomplete...You are missing the GUID for the Class library in the C# managed section...Using a simple test I wrote, this should be something as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;
namespace XMLConfig
{
[Guid("4e8f4284-38dc-424f-b49a-52c4b00bb544")]
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IXMLConfig
{
String ThisIsATest();
}
[ClassInterface(ClassInterfaceType.None)]
[Guid("ef0f2a89-2b2e-4550-bae0-4a8c2898a3df")]
[ComVisible(true)]
public class Config : IXMLConfig
{
public String ThisIsATest()
{
string str = "This is a test";
return str;
}
}
}
Basically, without the ClassInterface GUID, you can't generate the proper .tlh definition section which contains the CLSID and IID used by CoCreateInstance in unmanaged.
You have also failed to explain to the user how they figure out which CLSID and IID to use from the .tlh file. There is also a smart pointer definition in the .tlh that they should be using. Taking a snipet of my .tlh file these are defined as follows:
//
// Named GUID constants initializations
//
extern "C" const GUID __declspec(selectany) LIBID_XMLConfig =
{0x40e64a6b,0xf6c4,0x4605,{0xaa,0x97,0x84,0xb8,0x38,0x37,0x27,0x0f}};
extern "C" const GUID __declspec(selectany) IID_IXMLConfig =
{0x4e8f4284,0x38dc,0x424f,{0xb4,0x9a,0x52,0xc4,0xb0,0x0b,0xb5,0x44}};
extern "C" const GUID __declspec(selectany) CLSID_Config =
{0xef0f2a89,0x2b2e,0x4550,{0xba,0xe0,0x4a,0x8c,0x28,0x98,0xa3,0xdf}};
Obviously, the CLSID is CLSID_Config and the IID is IID_IXMLConfig. Other users should have different class id's and interface id's listed.
Last point, is the use of the smart pointer to the interface. In my .tlh file it is specified in the following section:
//
// Smart pointer typedef declarations
//
_COM_SMARTPTR_TYPEDEF(IXMLConfig, __uuidof(IXMLConfig));
Now we have all the weapons we need to tackle the CoCreateInstance in the unmanaged dll...
void foxcache_server_state::TestManagedInterface()
{
IXMLConfig * pIXMLConfig = NULL;
CoInitialize(NULL);
HRESULT hr = CoCreateInstance(CLSID_Config,NULL,CLSCTX_INPROC_SERVER,IID_IXMLConfig, reinterpret_cast <LPVOID *>(&pIXMLConfig));
if (!FAILED(hr))
{
//LPCTSTR str = new wchar_t[100];
CString str;
str.Append(pIXMLConfig->ThisIsATest());
}
CoUninitialize();
if(pIXMLConfig) pIXMLConfig->Release();
}
I have also noticed you forgot the proper syntax for reinterpret_cast, which should be something along the lines:
reinterpret_cast <LPVOID *>(&pIXMLConfig)
I think if one is writting interop assemblies, one should follow proper syntax and give more descriptive pointers to the uninitiated...Just my two cents...
Cheers
Mario Ghecea
Senior Software Engineer
Fox Interactive Media
-- modified at 17:15 Tuesday 5th June, 2007
|
|
|
|

|
Hi,
Thanks for your article. But I get the following compilation errors in my C Application which is using the interface. The following is the error.
'IMyInterface' : undeclared identifier.
I looked at the tlh file and did not find anything for IMyInterface. Am I missing something. Also the
using namespace std
gives an error. Will appreciate if you could attach your source files with this.
Thanks again.
|
|
|
|

|
Hi
Could you please let me know how do i take care of conversion of the data structures
|
|
|
|

|
thanks for the article.
i've created a web service in embedded C++ using ATL, which in runtime should link to dll written in C#.
i tried using LoadLibrary() and GetyProcAddress() but i failed.
do u know a method to achieve this?
cheers
|
|
|
|

|
This is a good article for boht beginners and intermediate C# programmers.
geratly appreciate the effort. congrats
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Gives a step by step procedure to access a Web Service from a C project through a C# library.
| Type | Article |
| Licence | |
| First Posted | 5 May 2005 |
| Views | 40,485 |
| Bookmarked | 26 times |
|
|