talarubi: (colorful pixelly)
[personal profile] talarubi

I thought I'd try the Visual .NET Forms stuff in MSVC++ 2005. I hoped I could get a decent GUI up and keep the rest of my code as clean, platform-independent C++.

Maybe I'm just stupid, but this is the simplest way I could find to get a std::wstring path out of an explorer icon dragged atop the form:

System::Void MainWin_DragDrop(
    System::Object^ sender,
    System::Windows::Forms::DragEventArgs^ e
) {
    array<String^>^ dropList =
        dynamic_cast<array<String^>^>(
            e->Data->GetData( DataFormats::FileDrop ));
    
    String^         filePathCLR = dropList[0];
    std::wstring    filePathSTL;
    	 
    // "Obvious" method
    //for each( wchar_t c in filePathCLR )
    //	filePathSTL.push_back(c);
    
    // Suggested method (MSVC documentation... srsly)
    using namespace Runtime::InteropServices;
    const wchar_t* filePathRaw = (wchar_t*)(
        (Marshal::StringToHGlobalUni( filePathCLR )).ToPointer()
    );
    filePathSTL = filePathRaw;
    Marshal::FreeHGlobal(IntPtr( (void*)filePathRaw ));
    
    m_MyObj = new SomeDocumentObject(filePathSTL);
    // ...
}

There are several interesting things to be seen here. I'm not sure what to think of them.

For one thing, this isn't even C++! They've added a whole chunk of C# as completely new syntax. Seen here are the ^ which is their garbage-collected heap pointer, array (which is a keyword and derives from System::Array), and a few bits of their class library. Also notice the for each, where each and in are context-sensitive (!!) as keywords.

Oh, and don't even get me started on the freaking casts.

This syntax is completely required. The form designer generates it, and their library depends on the special references. That's because if you compile in "safe" mode, hoping to run under someone's tight security policy (and they encourage you to)... pointers aren't even allowed!

Naturally, this breaks just about every useful piece of C and C++ out there, including the all-important STL which sets C++ apart. While I understand the security motivation, I think this is misleading on MS's part, since at that point, it's not C++ anymore! It's just another dialect of C#, or maybe C# with templates. :/

I could simply compile in "pure" mode (effectively, insecure bytecode), and use the STL with the bureaucracy above, but it's still annoying. Mmmfeh.

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

talarubi: (Default)
Talarubi

January 2007

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
28293031   

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 9th, 2025 06:50 am
Powered by Dreamwidth Studios