?
Solved

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

Posted on 2008-10-13
4
Medium Priority
?
292 Views
Last Modified: 2013-11-20
Hello,

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

Im ataching dll code.

THanks
#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);
pTopDocument->Release();
 
if (SUCCEEDED(hr)) {
 
// Get Container's Embeddings Enumerator
code = 3;
IEnumUnknown* pEnumerator;
hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);
pContainer->Release();
 
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);
pFrameIHTMLElement->Release();
 
if (SUCCEEDED(hr)) {
 
// Get IHTMLElement's Document IDispatch Interface
code = 6;
IDispatch* pFrameDocument;
pFrameWebBrowser->get_Document(&pFrameDocument);
pFrameWebBrowser->Release();
 
if (pFrameDocument != NULL ) {
 
// Get Frame's Docuument IDispatch Interface
code = 7;
IHTMLDocument2* pFrameIHTMLDocument2;
hr = pFrameDocument->QueryInterface(IID_IHTMLDocument2, (void**)&pFrameIHTMLDocument2);
pFrameDocument->Release();
 
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->Release();
 
} // pFrameIHTMLDocument2
} // pFrameDocument
} // pFrameIHTMLElement
} // pFrameWebBrowser
pUnk->Release();
} // for pEnumerator
} // pEnumerator
pEnumerator->Release();
} // 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"
 
EXPORTS
    ; Explicit exports can go here
	GetFrameDocuments @1

Open in new window

0
Comment
Question by:epagos7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 9

Accepted Solution

by:
Misbah earned 2000 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:

http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
0
 

Author Comment

by:epagos7
ID: 22705637
mmm,

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#
0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 22706598
System.Long should correspond to HRESULT, I am however not quite certain regarding the IWebBrowser2 interface however this thread might be helpful http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2463777&SiteID=1 
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

771 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