user5949771 user5949771 - 2 months ago 9
C++ Question

Reference C# library in C++ Program, But can't other PC

I made ClassLib to use C#.
And made another C++ win32 project.
This C++ Program Import C#'s tlb and can call lib's method successfuly.
But just work in development PC..

I have two file that is exe(made by c++) and dll(made by c#).
if i lay two file same folder on my development PC. It is work Successfuly.
But if i do same thing on another PC. That make happen.
CoCreateInstance() return null.

I don't know what is reason this problem..

Please teach me If you 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 is returned null if excuted on other pc.

This is 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.

Answer

Have you registered the library as a COM component? each COM library should be register on the target machine. in development system, registration will be done by visual studio when building the project.
You can do it manually or by installer scripts on the client machine.
the RegAsm.exe tool will help you! just type:
regasm.exe fileName.dll /tlb:fileName.Tlb
It will generate .TLB from dll and registers it on the machine.
and if you want register it manually (without regasm) take a look at this link:
https://msdn.microsoft.com/en-us/library/h627s4zy%28v=vs.110%29.aspx

Comments