[SOLVED]Windows Forms Again

Hi everyone,

I have come across a problem while working with Windows Forms in Visual C++ 2005. I will include the relevant snippet of my code below. Basically, I am passing the text from two textboxes into a function as parameters. The debugger correctly reads the values, however the function fails, saying that the values are "null".
Hopefully, someone here will be able to help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private: System::Void btnCrypt_Click(System::Object^  sender, System::EventArgs^  e) {
				 System::String ^ textToEncrypt;
				 System::String ^ inputKey;
				 char* inp;
				 char* key;
				 char* out;
				 int textLength;
				 int keyLength;

				 if(tbInput->Text != "" && tbKey->Text != "")
				 {
					 //encrypt
					 textToEncrypt = tbInput->Text;
					 inputKey = tbKey->Text;
					 inp = (char*)(void*)Marshal::StringToHGlobalAnsi(textToEncrypt);
					 key = (char*)(void*)Marshal::StringToHGlobalAnsi(inputKey);
					 textLength = textToEncrypt->Length;
					 keyLength = inputKey->Length;

					 out = Crypt(inp, textLength, key, keyLength);

					 System::String ^ outputText = gcnew System::String(out);

					 tbOutput->Text = outputText;
				 }
				 else
				 {
					 MessageBox::Show("Please enter some text and a key.","Error",
						 MessageBoxButtons::OK,MessageBoxIcon::Error);
				 }
			 }

			 char* Crypt(char*text, int len, const char* seed, int sl)
			 {
			 	 int i = 0;
			 	 char* ret;
			 	 
				 for(i = 0; i < len; i++)
				 {
				   ret[i] = text[i] ^ seed[i % sl];
				 }
				 return ret;
			 }


Regards,
Chris Matthews
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
char* Crypt(char*text, int len, const char* seed, int sl)
{
        int i = 0;
        char* ret;  //  <-- pointer to nothing!!
			 	 
        for(i = 0; i < len; i++)
        {
               ret[i] = text[i] ^ seed[i % sl];  // <-- using the pionter to nothing
        }
        return ret;
}
Last edited on
Ah... excuse my stupidity, but have you any idea on how to make it work? It worked as a console app when I was using Dev-Cpp...

Well actually, in Dev-Cpp I dynamically allocated memory for it using malloc. However, I just realised that I removed malloc as it didn't appear to work with .NET. Any solution?

Thanks,
Chris


It's all OK, I worked it out. I replaced
char* ret;
with
char* ret = new char;

Thanks again, Grey Wolf!
Chris
Last edited on
Topic archived. No new replies allowed.