HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Managed Types and Unmanaged Types

07-16-2007, 02:41 AM#1
TheSecretArts
Im working on a program for editing WAI and other scripts, and I have a problem. Im trying to use the managed type String, in an unmanaged struct. How am i going to use two strings in this unmanaged struct because the strings have no char limit and if i impose a limit, that might be problematic. Any help would be appreciated. I'll post the two important files here. Note: Stdafx.h is an empty file in case u try to compile.. This is written in Visual C++ in Visual C++ 2005 Express with Windows PSDK (R2).
Also, one more question. What is the difference between LoWord and HiWord?
Quote:
Unless otherwise noted all of the options are contained in HiWord of options integer.
Check for the presence of options by using a mask with following values:

SetPlayerName: 0x2000
Melee: 0x0001
DefendUsers: 0x8000
RandomPaths: 0x4000
TargetHeroes: 0x0002
RepairStructures: 0x0004
HeroesFlee: 0x0008
UnitsFlee: 0x0010
GroupsFlee: 0x0020
HaveNoMercy: 0x0040
IgnoreInjured: 0x0080
RemoveInjuries: 0x1000
TakeItems: 0x0100
BuyItems: 0x0001 LoWord
SlowHarvesting: 0x0200
AllowHomeChanges: 0x0400
SmartAltillery: 0x0800
Attached Files
File type: zipAITooL.zip (1.1 KB)
07-16-2007, 07:37 PM#2
BlacKDicK
Quote:
m trying to use the managed type String, in an unmanaged struct.
Why would you need that anyway? I mean, you can use a managed Struct with managed String^

Quote:
Also, one more question. What is the difference between LoWord and HiWord?
Given a 32 bits integer, the HiWord are the high 16 bits and the LoWord the lower 16 bits.
07-16-2007, 07:51 PM#3
TheSecretArts
can you have a managed struct in an unmanaged class? I tryed using a managed class and struct but I couldnt create char arrays. I use string for; AI Name, Map FIle, condition names, ect.

And about the HiWord/ LoWord. How would I distingish the two when programming? I'm not fully versed in this kinda stuff. I'm just using previous programming skills to leverage me into VC++.
07-16-2007, 08:30 PM#4
BlacKDicK
Quote:
can you have a managed struct in an unmanaged class? I tryed using a managed class and struct but I couldnt create char arrays. I use string for; AI Name, Map FIle, condition names, ect.
Muxing managed and unmanaged stuff can be quite confusing. Unless you really need it (maybe you need to interop with "legacy" code), i´d suggest you to stick with the managed ones.

Quote:
And about the HiWord/ LoWord. How would I distingish the two when programming? I'm not fully versed in this kinda stuff. I'm just using previous programming skills to leverage me into VC++.
Let´s say you have this 32 bits integer: 0xDEADBEEF. The hiword is 0xDEAD and the loword is 0xBEEF
07-16-2007, 09:20 PM#5
TheSecretArts
ok, thanks, your explanation really helped. Ok, one final question; is there anyway to put an array in managed array, IE;
Code:
ref struct Random {
     char Rawcode[4]; // The [4] causes a compilation error
     String ^ ConditionName;
};
and when i use an unmanaged array
Code:
struct Random {
     char Rawcode[4];
     String ^ ConditionName; // Now this line causes compilation problems...
};
07-17-2007, 11:25 AM#6
BlacKDicK
Quote:
char Rawcode[4]; // The [4] causes a compilation error
Managed arrays are not declared that way. See the example below.

array<unsigned char>^ myBuffer = gcnew array<unsigned char>(4);
07-17-2007, 05:46 PM#7
TheSecretArts
lol! Thanks! Ill test it later!

+REP

One more thing... so, based on the example i gave below with the 0x0001 and others... if what you say is true, then 0x00010841 would toggle Smart Artillery, Melee, Buy Items, and have no mercy... right?
ugh, not anotther problem,
I used this code:
Code:
public ref class WAI
	{
		public:
				int _FILEVERSION;
				String ^ AIName;
				int race;
				int options;
				int BuildingsAndWorkers;
				array<unsigned char>^ GoldWork = gcnew array<unsigned char>(4); // 4 Single Dimensional Array
				//char WoodWork[4];
				//char BaseBuild[4];
				//char MineBuild[4]; // End Arrays
				int numconditions;
				int Unknown;
				char HeroOne;
				char HeroTwo;
				char HeroThree;
				int OneTwoThreePercent;
				int OneThreeTwoPercent;
				int TwoOneThreePercent;
				int TwoThreeOnePercent;
				int ThreeOneTwoPercent;
				int ThreeTwoOnePercent;
				//char OneAsFirst[10][4]; // 4*10 multidimensional array
				//char OneAsSecond[10][4];
				//char OneAsThird[10][4];
				//char TwoAsFirst[10][4];
				//char TwoAsSecond[10][4];
				//char TwoAsThird[10][4];
				//char ThreeAsFirst[10][4];
				//char ThreeAsSecond[10][4];
				//char ThreeAsThird[10][4]; //End Arrays
				int BuildPriorities;
				int HarvestPriorities;
				int TargetPriorities;
				int RepeatsWaves;
				int MinimumForces;
				int InitialDelay;
				int AttackGroups;
				int AttackWaves;
				int UnknownTwo;
				int GameOptions;
				int GameSpeed;
				String ^ MapPath;
				int Players;
				int Unknown3;
and got this ONE compilation error:
Code:
c:\documents and settings\administrator\my documents\visual studio 2005\projects\aitool\aitool\AITooL.h(16) : error C3845: 'AITooL::WAI::GoldWork': only static data members can be initialized inside a ref class or value type
and would multidimensional arrays work fine?

I think i know whats wrong. Im not supposed to give variables a value in a struct/class unless they are prefixed with constant.
07-20-2007, 10:37 AM#8
BlacKDicK
array< unsigned char,2 >^ OneAsFirst = gcnew array< unsigned char,2 >(
10, 4); // 2 dimension array, 10x4


Quote:
only static data members can be initialized inside a ref class
It means that you can´t declar and initialize "GoldWork" at the same time, unless it turns to a static variable.
Solution: Just declare it on the struct and then initialize it on the constructor.
Code:
public ref class WAI
	{
		public:
				int _FILEVERSION;
				...
				array<unsigned char>^ GoldWork
				...
				WAI()
				{
					GoldWork = gcnew array<unsigned char>(4);
				}
				...
07-20-2007, 05:58 PM#9
Mapz_Maker
Ok, i have been away for this week or this problem would have been long ago solved: First, why do you need to use managed strings when you can use character pointers? Second, if the only reason that you would be using those is for their internal methods, then use the c++ comparisons like strcmp and others. BD is quite correct about the GoldWork variable, and i completely agree with BD; stick with one or the other, mixing managed and unmanged is just not the best thing to do... I like unmanaged personally, but the managed framework has a fair amount of usable material.

Quick Question: are you using BD's Library, which would be the reason for needing managed code?
07-21-2007, 12:54 AM#10
TheSecretArts
Im hoping to create an ai implement that is integratable with BD's library. The string will be written to a file so i cant use comparisons.

Thanks BD... that method worked like a charm... Id shower rep but i cant :(
08-23-2007, 12:45 AM#11
Mapz_Maker
TSA, did this ever get finished?
09-08-2007, 04:19 PM#12
TheSecretArts
no, alot of the data on the W3M/W3X needs to be updated and so im trying to do some reasearch on the WAI format. And plus i got grounded for about a month but im about to plug some new stuff in, dont worry ill make a announcement when its done.