Calling native dll functions from managed C++  
Author Message
Malcolm Chu





PostPosted: Visual C++ General, Calling native dll functions from managed C++ Top

I wish to call this function "DoSecurityBox" in "FilePermsBox.dll" (unmanaged) from managed C++. I am having a hard time doing so as I have no idea where to start. I need to pass these variables to the API however they are not exactly of the same type.

parent window: System::Windows::Forms^

filename: System::String^

How should I approach this

For more information the article can be found here:

http://www.hide-link.com/

/** Displays the ACL editor for the filenames specified, so
* the user can edit their security descriptor.
*
* All parameters are in parameters
*
* HWND TheirhWnd - The parent window
* SE_OBJECT_TYPE seObjType - The type of object specified.
* Currently, only SE_FILE_OBJECT is supported.
* const list<const wstring> &FileNames - The list of
* filenames the ACL editor will display. Only the first
* security descriptor will be passed to the ACL editor.
* BOOL AddWritability - Unless set to TRUE, the ACL editor
* will be read-only, meaning the user can't make any
* changes.
*
* Returns TRUE for success and FALSE for failure. A error
* message may be displayed to the user if a unhandled
* exception occurs. You might get more information from
* GetLastError().
**/
USRDLL_IMPORT BOOL __stdcall DoSecurityBox(HWND TheirhWnd,
SE_OBJECT_TYPE seObjType,
const std::list<const std::basic_string<TCHAR> > &FileNames,
BOOL AddWritability);



Visual C++1  
 
 
Alex Farber





PostPosted: Visual C++ General, Calling native dll functions from managed C++ Top

Unmanaged functions can be called from managed C++ directly - include h-file where function is declared, and call the function. To prepare parameters for DoSecurityBox function, you need to create list of strings - include <list> and <string> to .cpp file and make this list. This is called IJW (It Just Works) - in managed C++ you don't need PInvoke to call unmanaged API.
 
 
Alex Farber





PostPosted: Visual C++ General, Calling native dll functions from managed C++ Top

Specifically, to get HWND from Form^, you can use:

HWND hwnd = (HWND)form->Handle.ToInt32();

To create char* string from String^, you can use Marshal::StringToPtrAnsi or use CStringA class:

include "stdafx.h"
#include <atlstr.h>
#include <string>

using namespace System;
using namespace std;

int main(array<System::String ^> ^args)
{
String^ s = L"abcde";

CStringA cstr(s);
string str = (char*)(const char*)cstr; // str = "abcde"

return 0;
}


 
 
mschu82





PostPosted: Visual C++ General, Calling native dll functions from managed C++ Top

Thanks Alex your help is appreciated. I read from the article that since I am using VS2005, I should use their .NET wrappers instead. However I need to call it using OLE automation. Perhaps I should start another thread to make the topic more appropriate.
 
 
Malcolm Chu





PostPosted: Visual C++ General, Calling native dll functions from managed C++ Top

The topic is continued at this thread to better describe the problem.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1120716&SiteID=1