Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / Strings in array not accessing properly (also some tips for returning strings?)

Author
Message
Clonkex
Forum Vice President
13
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 4th Aug 2011 13:22
Hey guys,

I haven't been on here for a while but now that I am I have a problem.

I am developing a plugin for DBPro. I have an array with a struct that contains several types of data. One of them is a string. I have a function to set the string in any array index to anything the user chooses. This seems to work. I also have a function to get the string back from the array. This doesn't seem to work. I think I worked out how to return strings, so never fear about that, but in getting the string from the array they come out as all garbled or only every second or third array. It's really weird and for the life of me I can't figure it out!

Here is the function that returns the string as best as I can remember it:



DeleteString does the CreateDeleteString function on it and dbpstring does something I don't understand (I think it was written by someone called "Threescore" or something). I think it changes it to a DWORD. I just found it on the forums.

If you can see what I've done wrong PLEASE tell me! I really really need help on this!

Thanks in advance,
Clonkex

Never lie -- The government doesn't like the competition.

Intel Core 2 Duo 2.8Ghz, Nvidia GeForce 9400 GT 512mb, 1gb ram.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Aug 2011 15:59
There's not really a lot here to go on - is 'returnstring' given a valid value? What does 'dbpstring' look like? Is value 'valid' for your array?

Clonkex
Forum Vice President
13
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 5th Aug 2011 08:50 Edited at: 5th Aug 2011 08:52
That is not exactly how the function looks, but I have tested by using the MessageBox command to view what variables are being passed into everything and they all match- but it still doesn't return the correct string.

In the function that fills the array I used MessageBox at the end to check whether the array was in fact filled and whether it was filled correctly. As far as I can see, it was filling the array perfectly.

I will reply again later with the exact code and also give you the dbpstring function so you can see everything.

EDIT: On a side note, how would you store strings in arrays? And how would you retrieve them?

Thanks,
Clonkex

Never lie -- The government doesn't like the competition.

Intel Core 2 Duo 2.8Ghz, Nvidia GeForce 9400 GT 512mb, 1gb ram.
Clonkex
Forum Vice President
13
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 6th Aug 2011 09:28 Edited at: 6th Aug 2011 09:32
I did some more tests and I STILL cannot figure it out - in fact, I'm more confused then ever!

Ok, so here is exactly the code needed to re-create the problem.

Slightly edited header file Main.h:


Very edited CPP file Main.cpp:


Very edited Resource.rc:


I am definitely calling DR START first so there's no problem there.

What confuses me most is that the MessageBox command with FIRST in it displays the string with the right number in it, but SECOND always displays the string with 9 in it - I have absolutely NO IDEA why it might be doing this!

Please, please, PLEASE tell me what I'm doing wrong!

Clonkex

Never lie -- The government doesn't like the competition.

Intel Core 2 Duo 2.8Ghz, Nvidia GeForce 9400 GT 512mb, 1gb ram.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 6th Aug 2011 17:38
This doesn't do what you think it does:

It points your 'stringresource' at a constant string. And as it's in a loop, it points all the array items at the same string.

This isn't the main source of your problem though - this is:


You have reserved space on the stack and filled it with some characters. You then assign the address of that space to your stringresource. Then on the next loop, you overwrite those characters and then assign the same address to your stringresource.

Also, as soon as you leave that function, that stack space is no longer yours, and the address cannot be used in a valid way - but you've made many semi-permanent records of that address in your array.

Bottom line - you don't understand C strings and should find a tutorial somewhere if you want to continue to use them.

Here's some working code (rearrange to fit your structure) using C strings:


Here's the same code changed slightly to use the C++ std::string class:


Clonkex
Forum Vice President
13
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 8th Aug 2011 01:27 Edited at: 8th Aug 2011 04:42
Oh....It all becomes clear....

I understand the concept of pointers, it just never occurred to me that LPSTR was a pointer. I knew it was a typedef of char *, but apparently I never made that final leap to realise the problem.

Oh, man, I'm an idiot.

Yeah, I knew the size of 'text' was way too big for any kind of sensible use, but I kept accidentally making it too small and getting "...the stack around the variable text was corrupted...", so I made it 1000.

Thanks for the all help, I REALLY appreciate it

Does the 'string' type automatically assign memory to fit the entire string in, however big? It seems like a much more DBPro-style approach, so if it's a viable method, I think I'll use the std::string method (I prefer, though, to put 'using std' up the top).

It should have all been so obvious. I can't believe I spent so much time on a simple error like that. Sometimes I just can't see something until it's pointed out to me.

Thanks again,
Clonkex

Never lie -- The government doesn't like the competition.

Intel Core 2 Duo 2.8Ghz, Nvidia GeForce 9400 GT 512mb, 1gb ram.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Aug 2011 16:03
Quote: "Does the 'string' type automatically assign memory to fit the entire string in, however big?"

Up to available memory, yes.

Clonkex
Forum Vice President
13
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Aug 2011 10:27
Thanks, that made it all work perfectly. You have a really good knowledge of C++ and DBPro.

New subject:

Did you know the find free object() command in Matrix1Utils doesn't work with BBBGui? It made BBBGui get an error when you close the window, but it may be fixed in the latest version of your plugins. I haven't tested yet.

Clonkex

Never lie -- The government doesn't like the competition.

Intel Core 2 Duo 2.8Ghz, Nvidia GeForce 9400 GT 512mb, 1gb ram.
Clonkex
Forum Vice President
13
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 12th Sep 2011 00:32
Quote: "it may be fixed in the latest version of your plugins. I haven't tested yet."


It isn't. It still breaks it in the latest version of both BBBGUI and Matrix1Utils.

Clonkex

Login to post a reply

Server time is: 2024-03-29 06:09:28
Your offset time is: 2024-03-29 06:09:28