Reference C# library in C++ Program, But can't other PC -
i made classlib use c#. , made c++ win32 project. c++ program import c#'s tlb , can call lib's method successfuly. work in development pc..
i have 2 file exe(made c++) , dll(made c#). if lay 2 file same folder on development pc. work successfuly. if same thing on pc. make happen. cocreateinstance() return null.
i don't know reason problem..
please teach me if know. c++ sources.---- stdafx.h
#pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #import "pc3dx.dll" no_namespace, rename("postmessage","cdmpostmessage") #import "mailctrl.tlb" no_namespace named_guids
mail.h
#include <iostream> #include <stdio.h> #include <stdlib.h> #pragma once struct mailinterface; class mail { public: mail(void); ~mail(void); bool sendmail(); _bstr_t m_smtpserver; int m_port; _bstr_t m_from; _bstr_t m_to; _bstr_t m_title; _bstr_t m_body; private: mailinterface *mailinter; };
mail.cpp
#include "stdafx.h" #include "mail.h" using namespace std; mail::mail(void) { } mail::~mail(void) { } bool mail::sendmail() { bool result; mailinter = null; coinitialize(null); hresult hr = cocreateinstance(clsid_mailctrll, null, clsctx_inproc_server, iid_mailinterface, reinterpret_cast<void**>(&mailinter)); mailinter->setmail(m_smtpserver ,m_port ,m_from ,m_to ,m_title ,m_body); result = (mailinter->send())?true:false; return result; }
&mailinter returned null if excuted on other pc.
this c# sources. ----
using system; using system.collections.generic; using system.linq; using system.text; using system.net.mail; using system.runtime.interopservices; namespace mailctrl { [guid("a5a420c2-f419-4063-b9a8-fc45737a2b37")] public interface mailinterface { bool setmail(string m_server, int m_port ,string m_from, string m_to, string m_title, string m_body); bool send(); bool checkarg(); string getresultmsg(); } [guid("fc979228-1f8b-4d04-8cd8-546b0b64e616")] public class mailctrll : mailinterface { private string m_server=""; private int m_port=0; private string m_from = ""; private string m_to = ""; private string m_title = ""; private string m_body = ""; private mailmessage mail=null; private smtpclient smtpserver=null; private string resultmsg = ""; public bool setmail(string s_server, int s_port ,string s_from, string s_to, string s_title, string s_body) { m_server = s_server; m_port = s_port; m_from = s_from; m_to = s_to; m_title = s_title; m_body = s_body; return true; } public bool send() { mail = new mailmessage(); try { smtpserver = new smtpclient(m_server); if (checkarg() == false) { return false; } mail.from = new mailaddress(m_from); mail.to.add(m_to); mail.subject = m_title; mail.subjectencoding = system.text.encoding.utf8; mail.body = m_body; mail.bodyencoding = system.text.encoding.utf8; smtpserver.port = m_port; smtpserver.deliverymethod = smtpdeliverymethod.network; smtpserver.send(mail); } catch (exception ex) { return false; } return true; } public bool checkarg() { if (string.isnullorempty(m_server)) { return false; } else if (m_port < 0) { return false; } else if (string.isnullorempty(m_from)) { return false; } else if (string.isnullorempty(m_to)) { return false; } else if (string.isnullorempty(m_title)) { return false; } else if (string.isnullorempty(m_body)) { return false; } return true; } public string getresultmsg() { return resultmsg; } } }
thank you.
have registered library com component? each com library should register on target machine. in development system, registration done visual studio when building project.
can manually or installer scripts on client machine.
regasm.exe
tool you! type:
regasm.exe filename.dll /tlb:filename.tlb
generate .tlb dll , registers on machine.
, if want register manually (without regasm) take @ link:
https://msdn.microsoft.com/en-us/library/h627s4zy%28v=vs.110%29.aspx
Comments
Post a Comment