Calling methods of unmanged (C++) DLL from Managed environment
Requirement in detail:
C++ DLL
unmanged C++ DLL and can be modified (source code available)
Some methods of a C++ class is exposed.
Managed environment;
C# application
Issue in detail:
we have known how to expose c functions but no clear picture about c++ functions.
No idea how to keep c++ class instance in c#(manged) environment.
Solution in detail:
In unmanged C++ DLL, introduced new set of C functions and exposed them.
We are dealing with c++ class , through this tunnel.
In C# application, introduced similar kind of class (with functions)
stored c++ class instance in IntPtr member.
code snippets: c++ dll
C++ class (TestClass)
#define UNMANAGEDDLL_API __declspec(dllexport)
class UNMANAGEDDLL_API TestClass
{
public:
TestClass(void);
~TestClass(void);
void PassInt(int value);
};
new interface of C functions
testClassCaller.h
#pragma once
#include "TestClass.h"
extern "C" UNMANAGEDDLL_API TestClass* CreateTestClass();
extern "C" UNMANAGEDDLL_API void DisposeTestClass(TestClass* pObject);
extern "C" UNMANAGEDDLL_API void CallPassInt(TestClass* pObject, int nValue);
testClassCaller.cpp
#include "TestClassCaller.h"
extern "C" UNMANAGEDDLL_API TestClass* CreateTestClass()
{
return new TestClass();
}
extern "C" UNMANAGEDDLL_API void DisposeTestClass(TestClass* pObject)
{
if (pObject != NULL)
{
delete pObject;
pObject = NULL;
}
}
extern "C" UNMANAGEDDLL_API void CallPassInt(TestClass* pObject, int nValue)
{
if (pObject != NULL)
{
pObject->PassInt(nValue);
}
}
Managed enviornment
C# wrapper class
class CSUnmanagedTestClass : IDisposable
{
#region PInvokes
[DllImport("test4.dll")] //test4.dll is unmanged DLL
static private extern IntPtr CreateTestClass();
[DllImport("test4.dll")]
static private extern void DisposeTestClass(IntPtr pTestClassObject);
[DllImport("test4.dll")]
static private extern void CallPassInt(IntPtr pTestClassObject,int nValue);
#endregion PInvokes
#region Members
private IntPtr m_pNativeObject;
#endregion Members
public CSUnmanagedTestClass()
{
this.m_pNativeObject = CreateTestClass();
}
#region IDisposable Members
public void Dispose()
{
//throw new NotImplementedException();
Dispose(true);
}
#endregion
protected virtual void Dispose(bool bDisposing)
{
if (this.m_pNativeObject != IntPtr.Zero)
{
DisposeTestClass(this.m_pNativeObject);
}
if (bDisposing)
{
GC.SuppressFinalize(this);
}
}
~CSUnmanagedTestClass()
{
Dispose(false);
}
#region Wrapper methods
public void PassInt(int nValue)
{
CallPassInt(this.m_pNativeObject, nValue);
}
#endregion Wrapper methods
}
C# main method:
static void Main(string[] args)
{
CSUnmanagedTestClass testclass = new CSUnmanagedTestClass();
testclass.PassInt(34);
testclass.Dispose();
}
More Detail:
Kith Tech Place
All about my life with computer software development related information.
Wednesday, August 17, 2011
Tuesday, June 21, 2011
Sharing local resource at remote destop.
Steps to do (though this is simple, we may forget)
1. Select "option" at first dialog box of "Remote Desktop Connection"
2. Go "Local Resources" tab and select "more" button.
3. Select check box for your local disks.
Once you logged to remote machine , you will see your local disk a map drive in File Explorer.
Steps to do (though this is simple, we may forget)
1. Select "option" at first dialog box of "Remote Desktop Connection"
2. Go "Local Resources" tab and select "more" button.
3. Select check box for your local disks.
Once you logged to remote machine , you will see your local disk a map drive in File Explorer.
Tuesday, May 10, 2011
Small tips on .pst(outlook personal folder files) files
How to repair corrupted .pst file
SCANPST tool which comes with office package can be used.
Path for Office 2010
C:\Program Files\Microsoft Office\Office14
Setting .pst files
set as default .pst file
In Outlook 2010
file ->info->Account Setting->Account Setting->email tab
change folder btn at bottom. give .pst file
file ->info->Account Setting->Account Setting->data file
add .pst and set as default.
How to repair corrupted .pst file
SCANPST tool which comes with office package can be used.
Path for Office 2010
C:\Program Files\Microsoft Office\Office14
Setting .pst files
set as default .pst file
In Outlook 2010
file ->info->Account Setting->Account Setting->email tab
change folder btn at bottom. give .pst file
file ->info->Account Setting->Account Setting->data file
add .pst and set as default.
Wednesday, March 23, 2011
Tuesday, February 01, 2011
Windows Installer Cleanup utility (MSICUU2.exe).
When some applicaitons are not properly uninstlled , we can use this utitly to uninstall them properly.
There is line in microsoft web as ;
While the Windows Installer Cleanup utility resolved some installation problems, it sometimes damaged other components installed on the computer. Because of this, the tool has been removed from the Microsoft Download Center
When some applicaitons are not properly uninstlled , we can use this utitly to uninstall them properly.
There is line in microsoft web as ;
While the Windows Installer Cleanup utility resolved some installation problems, it sometimes damaged other components installed on the computer. Because of this, the tool has been removed from the Microsoft Download Center
Subscribe to:
Posts (Atom)