user5949771 user5949771 - 1 year ago 61
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.----

#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


#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#pragma once

struct MailInterface;

class Mail

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;


MailInterface *mailInter;



#include "stdafx.h"
#include "Mail.h"

using namespace std;



bool Mail::SendMail()
bool result;
mailInter = NULL;

HRESULT hr = CoCreateInstance(CLSID_MailCtrll,


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
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();

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();

smtpServer = new SmtpClient(m_Server);

if (CheckArg() == false)
return false;

mail.From = new MailAddress(m_From);
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;

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 Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download