But I run into problems whenever I try to register the DLL. It suggests that I add [ClassInterface(ClassInterfaceType.None)] to my class, which I then did, and it didn't work.
Does anyone have a code sample with a C# DLL accessed from C++ which works? Preferably a solution with the C# dll and another one with the C++ that accesses it.
Other suggestions/hints/articles are also welcome.
But I run into problems whenever I try to register the DLL. It suggests that I add [ClassInterface(ClassInterfaceType.None)] to my class, which I then did, and it didn't work.
Don't know where your problem is, but the MS sample builds just fine and works like charm.
Code:
E:\Temp\678>make.bat
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft (R) .NET Framework Assembly Registration Utility 4.0.30319.1
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully
Assembly exported to 'E:\Temp\678\ManagedDLL.tlb', and the type library was registered successfully
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
client.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:client.exe
client.obj
At the moment, when I try to register the DLL, my command prompt looks like this:
C:\SSRoot\FaellesIndbetaling\Common\POAHandler\POAHandler>regasm .\bin\Debug\POA
Handler.dll /tlb:C:\SSRoot\FaellesIndbetaling\Common\POAHandler\POAHandler\poaha
ndler.tlb /codebase
Microsoft (R) .NET Framework Assembly Registration Utility 4.0.30319.1
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.
Types registered successfully
Type library exporter warning processing 'POAHandler.POA_serviceref.POAServiceCl
ient, POAHandler'. Warning: Type library exporter encountered a type that derive
s from a generic class and is not marked as [ClassInterface(ClassInterfaceType.N
one)]. Class interfaces cannot be exposed for such types. Consider marking the t
ype with [ClassInterface(ClassInterfaceType.None)] and exposing an explicit inte
rface as the default interface to COM using the ComDefaultInterface attribute.
Assembly exported to 'C:\SSRoot\FaellesIndbetaling\Common\POAHandler\POAHandler\
poahandler.tlb', and the type library was registered successfully
And when I try to import things in my c++ project, it claims that it knows nothing about my class or interface.
Csharp code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POAHandler.POA_serviceref;
using System.Runtime.InteropServices;
/*
*
*
* C:\SSRoot\FaellesIndbetaling\Common\POAHandler\POAHandler>regasm .\bin\Debug\POA
Handler.dll /tlb:C:\SSRoot\FaellesIndbetaling\Common\POAHandler\POAHandler\poaha
ndler.tlb /codebase
*
* */
namespace POAHandler
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
public interface IPOA_Handler
{
bool CreatePoaJob(string POAkoe, string OpgOverskrift, string OpgTekst, string cpr = "", string cvr = "", string forsnr = "");
void FindPoaJob();
}
//[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
// InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
//public interface POA_HandlerEvents
//{
// //pt. tom
//}
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None)/*,
ComSourceInterfaces(typeof(POA_Handler))*/]
public class POA_Handler : IPOA_Handler
{
public bool CreatePoaJob(string POAkoe, string OpgOverskrift, string OpgTekst, string cpr = "", string cvr = "", string forsnr="")
{
POAServiceClient cli = new POAServiceClient();
var req = new OpretOpgaveRequest();
req.Header = new Header();
//req.Header.Action = "Test";
//req.Header.Caller = "Tester";
//req.Header.MessageID = Guid.NewGuid();
//req.Header.TimeStamp = DateTime.Now;
//req.Header.User = "Tester";
req.Opgave = new Opgave();
req.Opgave.Emne = EmneEnum.AENDRING; //Fast værdi, iflg. Klaus Munk
req.Opgave.Forloeb = ForloebEnum.UKENDT; //Giver i Bevakningsorsak: ”Auto”. Der burde stå FBA, men dette er (vist?) ikke muligt.
req.Opgave.Aarsag = OpgOverskrift;
req.Opgave.Afdeling = POAkoe; //POA kø
req.Opgave.Oprettelsesdato = DateTime.Now;
req.Opgave.Cpr = cpr;
req.Opgave.Cvr = cvr;
//req.Opgave.Aftalenummer = "1";
//req.Opgave.Agenturnummer = "1";
req.Opgave.Forespoergsel = OpgTekst;
req.Opgave.Forsikringsnummer = forsnr;
//req.Opgave.Skadesnummer = "1";
var resp = cli.OpretOpgave(req);
return !string.IsNullOrEmpty(resp.OpgaveIdPOA);
}
public void FindPoaJob() //Til brug når POA får denne mulighed
{
}
}
}
C++ code:
Code:
// POA_IF.cpp : Implementation of DLL Exports.
// Note: Proxy/Stub Information
// To build a separate proxy/stub DLL,
// run nmake -f POA_IFps.mk in the project directory.
#include "stdafx.h"
#include "resource.h"
#include <initguid.h>
#include "POA_IF.h"
#include "POA_IF_i.c"
#import "..\poahandler\poahandler\poahandler.tlb" raw_interfaces_only
CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
class CPOA_IFApp : public CWinApp
{
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPOA_IFApp)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
//{{AFX_MSG(CPOA_IFApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
void CreatePOAJob(CString JobTitel);
};
BEGIN_MESSAGE_MAP(CPOA_IFApp, CWinApp)
//{{AFX_MSG_MAP(CPOA_IFApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CPOA_IFApp theApp;
BOOL CPOA_IFApp::InitInstance()
{
_Module.Init(ObjectMap, m_hInstance, &LIBID_POA_IFLib);
return CWinApp::InitInstance();
}
int CPOA_IFApp::ExitInstance()
{
_Module.Term();
return CWinApp::ExitInstance();
}
/////////////////////////////////////////////////////////////////////////////
// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
return _Module.GetClassObject(rclsid, riid, ppv);
}
/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
// registers object, typelib and all interfaces in typelib
return _Module.RegisterServer(TRUE);
}
/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
return _Module.UnregisterServer(TRUE);
}
void CPOA_IFApp::CreatePOAJob(CString JobTitel)
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
IPOA_Handler pPOA(__uuidof(POA_Handler));
bool lResult = false;
// Call the Add method.
pPOA->CreatePoaJob("DTPFIR", "FBA siger Hello World CPR, CVR, ForsNr", "Test Opgave fra FBA", "1504650258", "12121212","94-123456", &lResult);
wprintf(L"The result is %d", lResult);
// Uninitialize COM.
CoUninitialize();
}
I had a webservice that I call from Csharp, and I would then make the Csharp code into something that can (also) be accessed using COM. Since I can see, that calling Csharp code from C++ can be a pain, I made a C++ class (also COM) that all the C++ projects can use.
Ah...it seems that what it is really nagging about is that it doesn't like the service reference in the Csharp project, apparently because it doesn't have [ClassInterface(ClassInterfaceType.None)]
I've tested a little more, and found that the problem isn't only with the C# code.
I removed the call to the webservice, and just let my function return true all the time. I recompiled, and this time it appeared that there was no problems when registering. However, when I tried to compile in C++, it kept claiming that the namespace for my C# doesn't exist...
Ah...it seems that what it is really nagging about is that it doesn't like the service reference in the Csharp project, apparently because it doesn't have [ClassInterface(ClassInterfaceType.None)]
I see. So, using COM as such is not the problem. BTW, I started writing post #6 before your post #4 arrived, so I hadn't already seen it at that point.
Except for the using declaration right at the top of your C# snippet, I don't see any reference to POA_serviceref in the code you posted at all, so I have no idea what its signature is. Is there any concrete need to expose it to COM at all? The usual reason why you get that warning from tlbexp is that the offending type simply can't be represented in terms of COM, so exposing it as-is probably is not of much use anyway. If you simply applied [ComVisible(true)] at assembly scope and POA_serviceref (which I assume is a class) just inherited that, then explicitly applying [ComVisible(false)] to it would be the simple natural solution.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
I've tested a little more, and found that the problem isn't only with the C# code.
I removed the call to the webservice, and just let my function return true all the time. I recompiled, and this time it appeared that there was no problems when registering. However, when I tried to compile in C++, it kept claiming that the namespace for my C# doesn't exist...
Well, it's all C# whatever you think of the situation. Once C++ claims there's no namespace, regasm has registered your dll without any COM compatible types. Please inspect the .tlh file that .tlb ultimately compiles to (or open the .tlb with oleview and see if the library entry is really there, i.e. TLB is not void). In case the header contains no namespace named by assembly name at all, the problem is C#. In case it does have the namespace (and the content is essentially what you need), please make sure your C++ sources reference proper namespace.
I've checked the generated tlh file, and it does include the correct namespace. (Starts out with namespace PoaHandler {)
I start the cpp file with
#import "..\poahandler\poahandler\poahandler.tlb" raw_interfaces_only
Now I get a number of other errors. The mentioned line 107 is:
IPOA_Handler pPOA(__uuidof(POA_Handler));
I dont have a problem later in the code, when I try to make a call to one of the member functions.
pPOA.CreatePoaJob((unsigned short*)"DTPFIR", (unsigned short*)"FBA siger Hello World CPR, CVR, ForsNr", (unsigned short*)"Test Opgave fra FBA", (unsigned short*)"1504650258",
(unsigned short*)"12121212",(unsigned short*)"94-123456", (short*)&lResult);
That line is okay.
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : error C2259: 'IPOA_Handler' : cannot instantiate abstract class due to following members:
c:\ssroot\faellesindbetaling\common\poaif\poa_if\debug\poahandler.tlh(37) : see declaration of 'IPOA_Handler'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IUnknown::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined
c:\program files\microsoft sdk\include\unknwn.h(113) : see declaration of 'QueryInterface'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'unsigned long __stdcall IUnknown::AddRef(void)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\unknwn.h(117) : see declaration of 'AddRef'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'unsigned long __stdcall IUnknown::Release(void)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\unknwn.h(119) : see declaration of 'Release'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::GetTypeInfoCount(unsigned int *)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2712) : see declaration of 'GetTypeInfoCount'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::GetTypeInfo(unsigned int,unsigned long,struct ITypeInfo ** )' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2715) : see declaration of 'GetTypeInfo'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::GetIDsOfNames(const struct _GUID &,unsigned short ** ,unsigned int,unsigned long,long *)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2720) : see declaration of 'GetIDsOfNames'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::Invoke(long,const struct _GUID &,unsigned long,unsigned short,struct tagDISPPARAMS *,struct tagVARIANT *,struct tagEXCEPINFO *,unsigned int
*)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2727) : see declaration of 'Invoke'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall PoaHandler::IPOA_Handler::CreatePoaJob(unsigned short *,unsigned short *,unsigned short *,unsigned short *,unsigned short *,unsigned short *,short *)'
: pure virtual function was not defined
c:\ssroot\faellesindbetaling\common\poaif\poa_if\debug\poahandler.tlh(43) : see declaration of 'CreatePoaJob'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall PoaHandler::IPOA_Handler::FindPoaJob(void)' : pure virtual function was not defined
c:\ssroot\faellesindbetaling\common\poaif\poa_if\debug\poahandler.tlh(51) : see declaration of 'FindPoaJob'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : error C2259: 'IPOA_Handler' : cannot instantiate abstract class due to following members:
c:\ssroot\faellesindbetaling\common\poaif\poa_if\debug\poahandler.tlh(37) : see declaration of 'IPOA_Handler'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IUnknown::QueryInterface(const struct _GUID &,void ** )' : pure virtual function was not defined
c:\program files\microsoft sdk\include\unknwn.h(113) : see declaration of 'QueryInterface'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'unsigned long __stdcall IUnknown::AddRef(void)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\unknwn.h(117) : see declaration of 'AddRef'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'unsigned long __stdcall IUnknown::Release(void)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\unknwn.h(119) : see declaration of 'Release'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::GetTypeInfoCount(unsigned int *)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2712) : see declaration of 'GetTypeInfoCount'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::GetTypeInfo(unsigned int,unsigned long,struct ITypeInfo ** )' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2715) : see declaration of 'GetTypeInfo'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::GetIDsOfNames(const struct _GUID &,unsigned short ** ,unsigned int,unsigned long,long *)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2720) : see declaration of 'GetIDsOfNames'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall IDispatch::Invoke(long,const struct _GUID &,unsigned long,unsigned short,struct tagDISPPARAMS *,struct tagVARIANT *,struct tagEXCEPINFO *,unsigned int
*)' : pure virtual function was not defined
c:\program files\microsoft sdk\include\oaidl.h(2727) : see declaration of 'Invoke'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall PoaHandler::IPOA_Handler::CreatePoaJob(unsigned short *,unsigned short *,unsigned short *,unsigned short *,unsigned short *,unsigned short *,short *)'
: pure virtual function was not defined
c:\ssroot\faellesindbetaling\common\poaif\poa_if\debug\poahandler.tlh(43) : see declaration of 'CreatePoaJob'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : warning C4259: 'long __stdcall PoaHandler::IPOA_Handler::FindPoaJob(void)' : pure virtual function was not defined
c:\ssroot\faellesindbetaling\common\poaif\poa_if\debug\poahandler.tlh(51) : see declaration of 'FindPoaJob'
C:\SSRoot\FaellesIndbetaling\Common\PoaIF\POA_IF\POA_IF.cpp(107) : error C2664: '__thiscall PoaHandler::IPOA_Handler::PoaHandler::IPOA_Handler(const struct PoaHandler::IPOA_Handler &)' : cannot convert parameter 1 from 'const struct _GUID' to 'const
struct PoaHandler::IPOA_Handler &'
Reason: cannot convert from 'const struct _GUID' to 'const struct PoaHandler::IPOA_Handler'
No constructor could take the source type, or constructor overload resolution was ambiguous
huong dan ca cuoc tren mang
huong dan ca cuoc online
huong dan ca do bong da
huong dan ca cuoc bong da
huong dan ca do tren mang
huong dan ca cuoc tren mang
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.