Link to home
Create AccountLog in
Avatar of John500
John500Flag for United States of America

asked on

Marshal::StringToHGlobalAnsi(Group3Display[0]).ToPointer() - does not convert data

Greetings:

I have a .Net 2002 project I'm trying to upgrade to 2005.  The following routine used to convert a System::String array named Group3Display[] to a basic string named Group3Data[]:

void Form1::Group3SocketProc()
{
const char* TempString;
string Group3Data[5];

for(int i = 0; i < 5; i++){
      TempString = (const char*)Marshal::StringToHGlobalAnsi(Group3Display[0]).ToPointer();
      Group3Data[i]      = TempString;
      Marshal::FreeHGlobal(IntPtr((void*)TempString));
}
...
...
}

Form1.h looks like this regarding the System::String Group3Display array:

Form1(void){

      this->CheckForIllegalCrossThreadCalls = false;
      InitializeComponent();
      ...
      ...

      this->Menu=mainMenu1;

}

private:

      System::String      *Group3Display[];
      ...
      ...
}

private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
 {
      Group3Display      = new String*[5];

      for(int i = 0; i < 5; i++)
            Group3Display[i] = "";

...
...
}

The basic string array - Group3Data[] is declared locally as seen above.  With each iteration of the For Loop, the Group3Data[] is populated with a '0' instead of the correct string array.

What's up with this?
Thanks!
Avatar of graye
graye
Flag of United States of America image

It's entirely clear what you're trying to do...   but wouldn't the folowing be more direct?

for (int i = 0; i < 5; i++){
      Group3Data[i]  = Group3Display[i];
}
Avatar of John500

ASKER

graye,

My understanding of why your method didn't work in version 2002 of .Net is for two reasons:

1)  ListBox21Msg() is a homemade function which accesses listBox21 - but listBox21 runs within one of the Form's threads.  That thread cannot be accessed by the thread running ListBox21Msg() unless it is done a specific way.

2)  The 'System::String' type cannot be assigned or converted to a basic string type.

If version of 2005 .Net has adjusted things so that both of these issues have gone away - I'm all ears.

Thanks
Avatar of Bob Learned
Are you trying take a string array, and join the members into a single string:

a
b
c
d
e

into a, b, c, d, e?

Bob
Avatar of John500

ASKER

Bob,

No, I'll save you long answer and simply say I need the contents of System::String array to be dumped to a basic string array:

System::String             basic string
a                                         a
b                                         b
c                                          c
d                                         d
e                                         e
Is this string array used outside or you project, or inside?  What is the need for a "basic string array"?

Bob
Avatar of John500

ASKER

>> What is the need for a "basic string array"?

First of all let me say I'm glad to see you took interest in this question because it actually has a lot to do with the other question you were helping me with:

https://www.experts-exchange.com/questions/23201031/How-to-create-virtual-functions-that-will-demonstrate-object-oriented-polymorphism.html

The reason there's a tie-in is because the project we are talking about here 'IS' the project from the other question when I said, "I already have a project that works to land aircraft"

Take a look at the picture below to see it:

What I did a couple of years ago with .Net C++ 2002 was much like putting a new chassis on an old engine.  The old engine would collect various data off the network (i.e. wind, radar ...) and feed it wherever necessary.  I took these old modules of code and fed them into a Windows Form application.

In the process of doing that I kept much of the old code that utilized the basic string class and which populated each of the groups you see below.  As data was collected off the network from these unmanaged parts of the code, that data was stored in Form1 global string arrarys and then transferred to Form1 groups using this routine:

void Form1::Group3SocketProc()
{
const char* TempString;
string Group3Data[5];

for(int i = 0; i < 5; i++){
      TempString = (const char*)Marshal::StringToHGlobalAnsi(Group3Display[0]).ToPointer();
      Group3Data[i]      = TempString;
      Marshal::FreeHGlobal(IntPtr((void*)TempString));
}
...
...
}

Hence, this is why I need to use the basic string array.  I was trying to save you this long answer but I can see it was unavoidable.  

At any rate, here I am in the year 2008 trying to update the 2002 project to 2005 and having problems.  Please don't confuse the other question (which dealt with my desire to create virtual classes and functions)  with this question.

I still would like to complete the other question but I'm happy with completing this one for now and the other whenever by whomever.



Thanks


panel.JPG
1) You have a managed string array.

2) That code only converts a single string to an unmanaged string type.

3) You would need to convert all the elements to an unmanaged string type.

Bob
Avatar of John500

ASKER

I messed up that last post when I included the following array as local:

string Group3Data[5];

The array above was actually global and I've been trying anything to get it to work as it once did in version 2002

The Group3Display[] array was declared in Form1.h and initialized during Form1_Load() - the details of it are seen in the post above -  http:#23225961  
Avatar of John500

ASKER

>> You would need to convert all the elements to an unmanaged string type.

Any suggestions?  Could I conatenate all the elements into one string and then parse the string?  Something like:

for(int i = 0; i < 5; i++){
      TempString = (const char*)Marshal::StringToHGlobalAnsi(Group3Display[0]).ToPointer();
      Group3DataString      =  Group3DataString + "," + TempString;
      Marshal::FreeHGlobal(IntPtr((void*)TempString));
}

Then parse Group3DataString .....


Avatar of John500

ASKER

>> You would need to convert all the elements to an unmanaged string type.

This is what I was doing with the unmanaged basic string array:

Group3Data[1]
Group3Data[2]
Group3Data[3]
...

Why isn't that acceptable?
Is that a string with 5 characters or a string array with 5 elements?

Bob
Avatar of John500

ASKER

It is declared just like the System:String array:

// basic string
string Group3Data[5];     // five elements

// system
System::String      *Group3Display[];
Group3Display      = new String*[5];       // five elements

during run-time the contents of the system string array can be seen below:





data-array.JPG
Avatar of John500

ASKER

I should have included the before and after shot of both arrays in that last picture.

Below you can see how that after the for loop is finished the Group3Data array has nothing but zeros in it:
group3data.JPG
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of John500

ASKER

yep - thats it.

Talk about code blind - i, yi, yi !

Thanks