Click here to Skip to main content
15,887,746 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Checking OS and Vista admin rights Pin
turkmeistr123-Apr-09 7:34
turkmeistr123-Apr-09 7:34 
GeneralRe: Checking OS and Vista admin rights Pin
Mark Salsbery23-Apr-09 8:04
Mark Salsbery23-Apr-09 8:04 
GeneralRe: Checking OS and Vista admin rights Pin
turkmeistr123-Apr-09 11:57
turkmeistr123-Apr-09 11:57 
GeneralRe: Checking OS and Vista admin rights Pin
Mark Salsbery23-Apr-09 12:29
Mark Salsbery23-Apr-09 12:29 
GeneralRe: Checking OS and Vista admin rights Pin
Mark Salsbery23-Apr-09 12:34
Mark Salsbery23-Apr-09 12:34 
GeneralRe: Checking OS and Vista admin rights Pin
turkmeistr123-Apr-09 13:03
turkmeistr123-Apr-09 13:03 
GeneralRe: Checking OS and Vista admin rights Pin
turkmeistr123-Apr-09 15:49
turkmeistr123-Apr-09 15:49 
QuestionMarshaling string array to unmanaged code Pin
Oldboy221-Apr-09 22:15
Oldboy221-Apr-09 22:15 
Hi,
My current project is to make wrapper classes of legacy mfc codes. I could easily marshal the String^ to CString like this:

bool LoadFile(String^ strPath)
{
// from managed to unmanaged using C++ Interop marshaling
pin_ptr<const wchar_t> pstrPath = PtrToStringChars(strPath);

// unmanaged declaration: bool LoadFile(CString path);
return m_pLegacyClass->LoadFile(pstrPath);
}

But string array is not a simple case. I marshaled all array members then copied the members to unmanaged memory. Here is the code.

bool LoadFiles(array<String^>^ arrayAviFilePath, int nAviFile)
{
// Array to marshal String^
array<IntPtr>^ arrayMarshalString = gcnew array<IntPtr>(nAviFile);

// Initialize unmanaged array
IntPtr arrayUnmanagedString = Marshal::AllocHGlobal(IntPtr::Size * nAviFile);

for (int i = 0; i < nAviFile; i++)
{
// Marshaling String^ to IntPtr
arrayMarshalString[i] = Marshal::StringToHGlobalUni(arrayAviFilePath[i]);

// Write to unmanaged memory
Marshal::WriteIntPtr(arrayUnmanagedString, i * IntPtr::Size, arrayMarshalString[i]);
}

// Use unmanaged array here
CString *pArrayAvi = static_cast<CString *>(arrayUnmanagedString.ToPointer());

bool bReturn = m_pLegacyClass->LoadFiles(pArrayAvi, nAviFile);

// Free unmanaged memory now
for (int i = 0; i < nAviFile; i++)
Marshal::FreeHGlobal(arrayMarshalString[i]);

Marshal::FreeHGlobal(arrayUnmanagedString);

return bReturn;
}

// legacy declaration
bool LoadFiles(CString arrayAviFilePath[], int nAviFile);


It works well anyway but is this best way?
AnswerRe: Marshaling string array to unmanaged code Pin
Mark Salsbery22-Apr-09 6:14
Mark Salsbery22-Apr-09 6:14 
GeneralRe: Marshaling string array to unmanaged code Pin
led mike22-Apr-09 6:27
led mike22-Apr-09 6:27 
GeneralRe: Marshaling string array to unmanaged code Pin
Oldboy222-Apr-09 16:24
Oldboy222-Apr-09 16:24 
GeneralRe: Marshaling string array to unmanaged code Pin
led mike24-Apr-09 4:13
led mike24-Apr-09 4:13 
QuestionQuestions about string manipulation Pin
TabascoSauce20-Apr-09 21:37
TabascoSauce20-Apr-09 21:37 
AnswerRe: Questions about string manipulation [modified] Pin
N a v a n e e t h20-Apr-09 23:19
N a v a n e e t h20-Apr-09 23:19 
GeneralRe: Questions about string manipulation [modified] Pin
TabascoSauce21-Apr-09 15:59
TabascoSauce21-Apr-09 15:59 
QuestionExecuting Code on App Exit Pin
turkmeistr120-Apr-09 9:16
turkmeistr120-Apr-09 9:16 
AnswerRe: Executing Code on App Exit Pin
Luc Pattyn20-Apr-09 9:22
sitebuilderLuc Pattyn20-Apr-09 9:22 
GeneralRe: Executing Code on App Exit Pin
turkmeistr120-Apr-09 9:32
turkmeistr120-Apr-09 9:32 
GeneralRe: Executing Code on App Exit Pin
Luc Pattyn20-Apr-09 9:40
sitebuilderLuc Pattyn20-Apr-09 9:40 
GeneralRe: Executing Code on App Exit Pin
turkmeistr120-Apr-09 10:01
turkmeistr120-Apr-09 10:01 
GeneralRe: Executing Code on App Exit Pin
Luc Pattyn20-Apr-09 10:03
sitebuilderLuc Pattyn20-Apr-09 10:03 
QuestionHow to copy/convert System::Object^(unsigned char) to local variable Pin
marcusab18-Apr-09 10:27
marcusab18-Apr-09 10:27 
AnswerRe: How to copy/convert System::Object^(unsigned char) to local variable Pin
N a v a n e e t h18-Apr-09 16:42
N a v a n e e t h18-Apr-09 16:42 
GeneralRe: How to copy/convert System::Object^(unsigned char) to local variable Pin
marcusab18-Apr-09 23:30
marcusab18-Apr-09 23:30 
GeneralRe: How to copy/convert System::Object^(unsigned char) to local variable Pin
N a v a n e e t h19-Apr-09 6:41
N a v a n e e t h19-Apr-09 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.