Skip to content

Managed C++/CLI

Discussions on Managed Extensions for Visual C++ .NET

This category can be followed from the open social web via the handle managed-c-cli@forum.codeproject.com

4.4k Topics 14.9k Posts
  • Converting and Concatenating Strings

    c++ csharp tutorial question
    2
    0 Votes
    2 Posts
    3 Views
    J
    There are no overloaded + or += operators for Strings in MC++, so one must use the plain member functions: String *String1 = S"C:\\files"; String *String2 = S"data.txt"; String1 = String::Concat(String1, S"\\", String2); //or much less efficiently... //String1 = String1->Insert(String1->Length, S"\\"); //String1 = String1->Insert(String1->Length, String2); char achCStr[20]; wchar_t *pGuts = PtrToStringArray(String1); int iChrs = ::WideCharToMultiByte(CP_ACP, 0, pGuts, String1->Length, achCStr, 20, NULL, NULL); achCStr[iChrs] = '\0'; PtrToStringArray() was posted earlier at http://www.codeproject.com/script/comments/forums.asp?forumid=3785&select=364715#xx364715xx[^] , and explained 2 posts earlier in that thread. String::Concat() returns a concatenated copy of between 2 and 4 strings, and since the lengths of all the Strings are calculated prior to assembling them into the returned String, should avoid multiple temporary copies usually associated with concatenation (nice!). Insert() works like strcat() when the start position is the same as the original String's length, but as you can see it requires an extra temporary String copy :(. It's undocumented, but when I do something like the above, I pass 'String1->Length+1' in WideCharToMultiByte(), which avoids having to add the terminating null character afterwards. Like CString and std::string, CLR Strings seem to always have a terminating null appended to them, and I have never seen a case where they haven't. Cheers
  • Looping sending me loopy!

    csharp c++ database visual-studio help
    3
    0 Votes
    3 Posts
    3 Views
    J
    monrobot13 already answered your question, but just to give a syntax example: for(i=0; Command->get_Chars(i) && (i < len); i++) { dyerstein wrote: Ha! And I though .NET was going to be cool!! I'm gaining a headache pretty fast! I keep a bottle of pain relievers around for the headaches (not that it helps much), but I can't say the transition to MC++ is exactly fun. C# is a much better fit for .Net, however MC++ is our only practical way to use C++ with it. Cheers
  • Memory leak in managed class!!!

    question c++ debugging performance help
    3
    0 Votes
    3 Posts
    2 Views
    J
    I feel your pain, since there is so little info on how __gc and __nogc stuff interacts in MC++. One thing is that any __gc class you create, should inherit from IDisposable when there are any heap (gc heap or regular heap) members to deallocate. Regular C++ destructors are not treated quite the same in __gc classes. For one thing, they are considered a last resort, and synonymous with Finalize methods defined in .Net. They are supposed to be declared protected to hide them from users, and should themselves just call the Dispose method that IDisposable descendants are supposed to implement. It is actually in the Dispose method where deallocation is supposed to occur (this all seems complicated, I know). Here is a simplified snippet from one of my __gc classes that implements IDisposable. I left in some comments from the .Net docs: public __gc class GcClass : public IDisposable { char *m_pBuf; public: GcClass() { m_pBuf = new char __nogc[BUF_SIZE]; } void Dispose() { //required by IDisposable to be made avail to users Dispose(true); //true indicates user-called // Take ourselves off of the Finalization queue to prevent // finalization code for this object from executing a second time. GC::SuppressFinalize(this); } protected: ~GcClass() { //CLR dtors are supposed to be protected Dispose(false); //DO NOT delete[] m_pBuf here! } // Dispose(bool disposing) executes in two distinct scenarios. // If disposing equals true, the method has been called directly // or indirectly by a user's code. Managed and unmanaged resources // can be disposed. // If disposing equals false, the method has been called by the // runtime from inside the finalizer and you should not reference // other objects. Only unmanaged resources can be disposed. virtual void Dispose(bool bWeAreDisposing) { if(this->m_pBuf) { //if Dispose has not been called yet if(bWeAreDisposing) { //If disposing, dispose all gc resources. //only release managed resources here } //Release unmanaged resources. If bWeAreDisposing is false (called by runtime), // only the following code is executed. //Note that this is not thread safe. Another thread could start // disposing the object after the managed resources are disposed, // but before the disposed flag is set to true (same as m_pBuf set NULL in this class). dele
  • Conversion problem

    c++ data-structures help tutorial question
    4
    0 Votes
    4 Posts
    3 Views
    J
    My pleasure. However, I forgot there were embedded < and > characters in my last post, so the code was clipped. Here is the correct code displayed as non-HTML: inline wchar_t * PtrToStringArray(System::String *s) { System::String __pin*pps = s; //pin to avoid 1-instruction GC hole in reinterpret_cast System::Byte __pin*bp = reinterpret_cast(s); if( bp != 0 ) bp += System::Runtime::CompilerServices::RuntimeHelpers::OffsetToStringData; return reinterpret_cast(bp); }; wchar_t *pStr = PtrToStringArray(Str); //pin and access array int iChrs = WideCharToMultiByte(CP_ACP, 0, pStr, Str->Length, pYourBuf, iBufLen, NULL, NULL); //do whatever with ANSI string in pYourBuf Cheers
  • Writing COM accesible objects in Managed C++

    c++ com tutorial question
    3
    0 Votes
    3 Posts
    4 Views
    J
    Here is the attribute again.... but visible this time <ClassInterface(ClassInterfaceType.None)>
  • C++ vs. Managed C++

    csharp c++ visual-studio performance question
    7
    0 Votes
    7 Posts
    3 Views
    N
    SanShou wrote: I am going to have to reasearch such as ngen'd In short, when you load a application that was developed under .NET it has to take the MSIL and run it through the JIT compiler to produce native code in memory. This is typically very fast and you really don't notice it. However there are times when you can use the n'gen utility to run the .NET application through the JIT compiler and have it produce the native code which you can persist to disk. Once the application has been n'gened you will no longer have the JIT compiling taking place, that is essentially all you gain/lose. Hope this helps. Nick Parker Not everything that can be counted counts, and not everything that counts can be counted. - Albert Einstein
  • Help!!!!!

    help csharp c++ visual-studio security
    1
    0 Votes
    1 Posts
    1 Views
    No one has replied
  • MFC app - porting

    csharp c++
    5
    0 Votes
    5 Posts
    3 Views
    M
    From a personnel point of view (and maybe project), i think i'm heading out to buy my first C# book. Thanks for your advice Max Matt G
  • Linker Errors

    c++ com help question
    2
    0 Votes
    2 Posts
    3 Views
    N
    Paul Ingles wrote: Any suggestions would be very much welcomed. Ok, after a little looking I came across this[^] knowledge base article, however you may look into this[^] KB article as well, hope this points you in the right direction. :) Nick Parker Not everything that can be counted counts, and not everything that counts can be counted. - Albert Einstein
  • setting dos screen prompt other than C:\

    2
    0 Votes
    2 Posts
    2 Views
    N
    from run in the start menu: command /k d: if you want to default to some directory, you can put it in a batch file. test.bat: d: cd d:\mydir\thisone\ then do: command /k test.bat - Nitron "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
  • ans required?

    com help question
    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • Database Problem ?

    database sysadmin security debugging help
    5
    0 Votes
    5 Posts
    2 Views
    N
    Thank you, that was exactly what I needed. :) Nick Parker Not everything that can be counted counts, and not everything that counts can be counted. - Albert Einstein
  • Ambiguous calls

    help c++ question
    3
    0 Votes
    3 Posts
    2 Views
    S
    One call is there in the <= operator. Other is //////////// const FpDate& rFpDateOperand1 = (GetDateEvaluationValue(uliRecordNumber, ComparisonExpression::OPERAND_LEFT_SIDE,0)); const FpDate& rFpDateOperand2 = (GetDateEvaluationValue(uliRecordNumber, ComparisonExpression::OPERAND_RIGHT_SIDE,0)); return (rFpDateOperand1 > rFpDateOperand2); ///////// Thank You
  • Form Handle

    wpf question
    3
    0 Votes
    3 Posts
    3 Views
    J
    Thanks Kannan, that was a great idea. I was able to use my form's CreateParams to create a handle with the method you posted. Unfortunately, there does not seem to be a viable way to get a CLR form to attach to the created handle as in MFC/ATL, so I'm left with a raw window without child controls, which knows nothing of CLR stuff. However, while subclassing I realised I could access the protected CreateHandle(), so I created a public method to call it externally. That worked, however it does not force handle creation for any of the child controls (as CreateControl() is supposed to), so I would have to subclass every one to create them too! This is an aggravating bug, since non-visible form creation is simple in C/C++, and CreateControl() is supposed to do this too. I remain open to further ideas, and will report this bug to Microsoft (though I'm sure they've heard this one before). Cheers
  • How to Change the Icon from the TitleBar

    graphics help tutorial
    3
    0 Votes
    3 Posts
    3 Views
    P
    Both answers work Perfect :)) I have used for the MenuBar a Image List, I guess it was wrong thinking that a 'ico' file is a renamed Bitmap file !! Peter.
  • C++ plotting routine

    data-structures c++ tutorial question
    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • Wrapping a unmanaged C++ class

    question c++
    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • fstream

    csharp c++ help
    4
    0 Votes
    4 Posts
    2 Views
    N
    Anonymous wrote: I am unsure of what file for the #using Nick didn't format it (;P), the following will need to be included for the source to work: #include "stdafx.h" #using <mscorlib.dll> #include <tchar.h> #include <fstream> using namespace System; Nick Parker May your glass be ever full. May the roof over your head be always strong. And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing
  • Can anybody help me?

    help csharp c++ database dotnet
    1
    0 Votes
    1 Posts
    1 Views
    No one has replied
  • Odd Error....

    c++ database sysadmin xml help
    4
    0 Votes
    4 Posts
    2 Views
    N
    Nick, First off thanks, I don't want to bug you however it doesn't seem like this forum gets as much attention as some of the others. Could you take a look at thread I posted above: http://www.codeproject.com/script/comments/forums.asp?msg=346059&forumid=3785#xx346059xx[^]. I have no idea why I am getting 0 back all the time. This code, once it works will be a class written in MC++ and instantiated in C# as a web service. I hadn't seen anything like it yet so I thought it would be interesting to try. Thanks. :) Nick Parker May your glass be ever full. May the roof over your head be always strong. And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing