How can i use a c++ compiled dll on c#?

Posted on 2008-10-13
Last Modified: 2013-11-20

Ive compiled a dll and i need it to use on c#, how can i do that?

Im ataching dll code.

#include "stdafx.h" // Required Precompiled Header

#include "unknwn.h" // IUnknown Interface

#include "Exdisp.h" // IWebBrowser2 Interface

#include "stdlib.h" // x

#include "mshtml.h" // MSHTML

BOOL APIENTRY getFrameDocumentById( HANDLE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved)


return TRUE;


extern "C" __declspec(dllexport) HRESULT CALLBACK GetFrameDocuments(long cbAddress,

IWebBrowser2* pTopWebBrowser) {


// ----- Initialize Variables ---


int code = 0;

HRESULT hr = 0;

UINT i = 0;

// Validate WebBrowser Argument (!NULL, top win, !busy, etc.)

if (pTopWebBrowser != NULL) {

// Get pTopWebBrowser's Document IDispatch Interface

code = 1;

IDispatch* pTopDocument;

hr = pTopWebBrowser->get_Document(&pTopDocument);

if (pTopDocument != NULL) {

// Get pTopDocument's Container OLE Interface

code = 2;

IOleContainer* pContainer;

hr = pTopDocument->QueryInterface(IID_IOleContainer, (void**)&pContainer);


if (SUCCEEDED(hr)) {

// Get Container's Embeddings Enumerator

code = 3;

IEnumUnknown* pEnumerator;

hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);


if (SUCCEEDED(hr)) {

// Iterate Container's Embeddings

IUnknown* pUnk;

ULONG uFetched;

for (i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); i++) {

// Get Embedded's IWebBrowser2 Interface

code = 4;

IWebBrowser2* pFrameWebBrowser;

hr = pUnk->QueryInterface(IID_IWebBrowser2, (void**)&pFrameWebBrowser);

if (SUCCEEDED(hr)) {

// Get Embedded's IHTMLElement Interface

code = 5;

IHTMLElement* pFrameIHTMLElement;

hr = pUnk->QueryInterface(IID_IHTMLElement, (void**)&pFrameIHTMLElement);


if (SUCCEEDED(hr)) {

// Get IHTMLElement's Document IDispatch Interface

code = 6;

IDispatch* pFrameDocument;



if (pFrameDocument != NULL ) {

// Get Frame's Docuument IDispatch Interface

code = 7;

IHTMLDocument2* pFrameIHTMLDocument2;

hr = pFrameDocument->QueryInterface(IID_IHTMLDocument2, (void**)&pFrameIHTMLDocument2);


if (SUCCEEDED(hr)) {

typedef void (__stdcall *FUNCPTR)(

IWebBrowser2 *pWebBrowser,

IHTMLDocument2 *pFrameDocument,

BSTR pbstr);

FUNCPTR vbFunc = (FUNCPTR)cbAddress;

wchar_t wchar[] = L"Test string";

vbFunc(pTopWebBrowser, pFrameIHTMLDocument2, SysAllocString(wchar));


} // pFrameIHTMLDocument2

} // pFrameDocument

} // pFrameIHTMLElement

} // pFrameWebBrowser


} // for pEnumerator

} // pEnumerator


} // pContainer

} // pTopDocument

} // Vaidate pTopWebBrowser

//return 0;

if (hr == 0) {

return i + 9900; // testing


else {

return code + 8800;


} // GetFrameWindows

Def File:

; Cross.def : Declares the module parameters for the DLL.

LIBRARY      "Cross"


    ; Explicit exports can go here

	GetFrameDocuments @1

Open in new window

Question by:epagos7

Accepted Solution

Misbah earned 500 total points
ID: 22705509
you need to declear the functions/methods you made in cpp in a c# class
and then you can use this c# class (wrapper for cpp) as any normal c# class.

see this example:

Author Comment

ID: 22705637

any help how can i declare and use the c++ func:

extern "C" __declspec(dllexport) HRESULT CALLBACK GetFrameDocuments(long cbAddress,
IWebBrowser2* pTopWebBrowser) {


Also i do not have HResult type on c#
LVL 19

Expert Comment

ID: 22706598
System.Long should correspond to HRESULT, I am however not quite certain regarding the IWebBrowser2 interface however this thread might be helpful 

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now