Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Calling Nishant S; Nick Parker ; anyone RE CString and /clr

Calling Nishant S; Nick Parker ; anyone RE CString and /clr

Scheduled Pinned Locked Moved Managed C++/CLI
c++helplearningcsharpdotnet
4 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Doug
    wrote on last edited by
    #1

    Hi folks Re: ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable I have found the source of the problem expressed in the thread "ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable": http://www.codeproject.com/script/comments/forums.asp?forumid=3785#xx309754xx but do not yet know why it occurs or how I can fix it. The problem arises with the /clr compiler setting. Here is the code to illustrate this: ///////////////////////////////////////////////////////// #include #include #include "try_ADONET.h" //#using //#using //#using // This is required for the ADO.NET Provider using namespace std; //using namespace System; int Main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; return 0; } //////////////////////////////////////////////////////// Without the /clr setting the array of CString busNumberSQL[2] is correctly contructed as is CString bus and the line busNumberSQL[1] = bus; works as expected. This can be seen by putting a breakpoint in at return 0; and seeing the variables in a Watch. Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet. .................. Next, add the compiler switch /clr and rerun the code. CString bus is constructed correctly BUT something weird happens in the construction of CString busNumberSQL[2]. Now, busNumberSQL[1] = bus; results in the address of bus as an integer being assigned to busNumberSQL[1], and look at the Watch details!! Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet Name: busNumberSQL; Value: {Length=2}; Type: ATL::CStringT > >[] ...(Note no dimension at end compared to the "no /clr" case. Next Watch line: [0] 2083454756 __int32 Next Watch line: [1] 3103464 __int32 My conclusion is that busNumberSQL[2] is not an array of CString. Of course, I need the /clr in order to use ADO.NET! It seems as though a CString works, but a CString array for some (unknown) cannot even be defined. Do you have any light for me? (I have not got into your previous post yet on Marshall) Best regards Doug. (In case you are wondering I am very much a beginner with C++ and .NET) Doug

    N 1 Reply Last reply
    0
    • D Doug

      Hi folks Re: ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable I have found the source of the problem expressed in the thread "ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable": http://www.codeproject.com/script/comments/forums.asp?forumid=3785#xx309754xx but do not yet know why it occurs or how I can fix it. The problem arises with the /clr compiler setting. Here is the code to illustrate this: ///////////////////////////////////////////////////////// #include #include #include "try_ADONET.h" //#using //#using //#using // This is required for the ADO.NET Provider using namespace std; //using namespace System; int Main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; return 0; } //////////////////////////////////////////////////////// Without the /clr setting the array of CString busNumberSQL[2] is correctly contructed as is CString bus and the line busNumberSQL[1] = bus; works as expected. This can be seen by putting a breakpoint in at return 0; and seeing the variables in a Watch. Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet. .................. Next, add the compiler switch /clr and rerun the code. CString bus is constructed correctly BUT something weird happens in the construction of CString busNumberSQL[2]. Now, busNumberSQL[1] = bus; results in the address of bus as an integer being assigned to busNumberSQL[1], and look at the Watch details!! Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet Name: busNumberSQL; Value: {Length=2}; Type: ATL::CStringT > >[] ...(Note no dimension at end compared to the "no /clr" case. Next Watch line: [0] 2083454756 __int32 Next Watch line: [1] 3103464 __int32 My conclusion is that busNumberSQL[2] is not an array of CString. Of course, I need the /clr in order to use ADO.NET! It seems as though a CString works, but a CString array for some (unknown) cannot even be defined. Do you have any light for me? (I have not got into your previous post yet on Marshall) Best regards Doug. (In case you are wondering I am very much a beginner with C++ and .NET) Doug

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Hello Doug I compiled and ran this program successfully. #include <Stdafx.h> #include <cstdlib> #include <atlstr.h> #using <mscorlib.dll> #using <System.dll> #using <system.data.dll> using namespace std; using namespace System; int main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; Console::WriteLine(busNumberSQL[1]); //shows "Hello" on my console return 0; } I have no idea why it's not working on your box. Regards, Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      D 2 Replies Last reply
      0
      • N Nish Nishant

        Hello Doug I compiled and ran this program successfully. #include <Stdafx.h> #include <cstdlib> #include <atlstr.h> #using <mscorlib.dll> #using <System.dll> #using <system.data.dll> using namespace std; using namespace System; int main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; Console::WriteLine(busNumberSQL[1]); //shows "Hello" on my console return 0; } I have no idea why it's not working on your box. Regards, Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        D Offline
        D Offline
        Doug
        wrote on last edited by
        #3

        Nish Thanx You are correct Console::WriteLine(busNumberSQL[1]); //shows "Hello" on my console works, as does cout << "busNumberSQL[1] = " << static_cast(busNumberSQL[1]) << endl; Just a pity you cannot, in this case set up the Watch window as a debugging tool by itself. Here is what someone wrote : Quote I see what you are talking about. Although it is not really a problem but just the way managed code and the CLR interacts with unmanaged code. When you use unmanaged code such as the case with the CString object in your example, and compile with the /clr, the managed executable makes references to the unmanaged objects internally using pointers to its memory location which falls outside the GC managed memory pool. As you have noticed when placing a watch on the unmanaged CString objects which show up as a __int32 type with a value that points to a memory address. There is nothing wrong with the example you gave in your email. The program still compiles, and the results it produces is as expected. The nice thing with C++.Net is the power of mixing managed and unmanged code in one executable. The power does not come without a price and that is your managed executable with parts unmanaged code, has the same risks as a completely unmanaged executable. The CLR does not offer any protection or resource management to unmanaged objects. So, use the power carefully. using your example above, if you add to following line to your code you'll see that it still produces the expected results. Console::Writeline (busNumberSQL[1]); or even, printf ("%s",busNumberSQL[1] ); Unquote http://www.codeproject.com/managedcpp/adonet_mcpp.asp?msg=311206#xx311206xx Regards Doug Doug

        1 Reply Last reply
        0
        • N Nish Nishant

          Hello Doug I compiled and ran this program successfully. #include <Stdafx.h> #include <cstdlib> #include <atlstr.h> #using <mscorlib.dll> #using <System.dll> #using <system.data.dll> using namespace std; using namespace System; int main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; Console::WriteLine(busNumberSQL[1]); //shows "Hello" on my console return 0; } I have no idea why it's not working on your box. Regards, Nish


          Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

          D Offline
          D Offline
          Doug
          wrote on last edited by
          #4

          Correction: cout << "busNumberSQL[1] = " << static_cast(busNumberSQL[1]) << endl; should read: cout << "busNumberSQL[1] = " << static_cast(busNumberSQL[1]) << endl; PS. Apologies for the multiple post - the error of my way has been pointed out to me and will not happen again. Doug

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups