After taking some small courses and using WPF and C# a bit I’ve decided to rewrite an application I have been working on. I have most of the functions working perfectly in a C++ DLL i created and imported into my WPF application.
I’m having a bit of trouble with a few of them though. One’s where I had previously passed variables from other functions or used dialogues and messageboxes.
Here is an example of one of the C++ functions i need to put into the DLL. The function generates a MD5 hash code of a list of files in a listbox that were added there by using the OpenFileDialog.
array<Byte>^ Hash()
{
array<Byte>^ Buffer = nullptr;
int x = 0;
for each(String^ Files in listBox2->Items)
{
try
{
IO::FileStream^ FileStream = gcnew IO::FileStream(Files, IO::FileMode::Open, IO::FileAccess::Read);
IO::BinaryReader^ BinaryReader = gcnew IO::BinaryReader(FileStream);
IO::FileInfo^ FileInfo = gcnew IO::FileInfo(Files);
System::Int64 TotalBytes = FileInfo->Length;
Buffer = BinaryReader->ReadBytes(safe_cast<System::Int32>(TotalBytes));
FileStream->Close();
delete FileStream;
BinaryReader->Close();
MD5^ md5 = gcnew MD5CryptoServiceProvider;
array<Byte>^ Hash = md5->ComputeHash(Buffer);
String^ FileHex = BitConverter::ToString(Hash);
listBox3->Items->Add(FileHex);
x = x + 1;
}
catch(Exception^ e)
{
MessageBox::Show(e->Message->ToString());
listBox1->Items->RemoveAt(listBox1->SelectedIndex);
}
}
return Buffer;
}
This code works perfectly in my C++ app that i made. So what i tried to do was take everything within the try statement and use that as the code for the method however my problem comes from the first line where obviously “Files” is a variable or at least i think it is where the issue is.
Is there a way i can still use this code as is and create a variable in C# and then pass it to this method?
I attempted to do so using the following code in my C# app
private void button2_Click(object sender, RoutedEventArgs e)
{
DllTest.Funtions Functions = new DllTest.Funtions();
foreach (String Files in listBox1.Items)
{
String File = Files;
File = Functions.HashFunction();
listBox2.Items.Add(File);
}
}
However when i run the application i only get the catch messages appearing in the listbox. this is the error in the compiler when i use the method “A first chance exception of type ‘System.ArgumentNullException’ occurred in mscorlib.dll”
Is there anyway i can do this without rewritting the method in C#?
Sorry if my code isn’t the greatest i’m still pretty new to C++ and C#
Consulting my psychic debugger, I’ve determined that you want this in C++/CLI:
and this in C#:
But my psychic debugger often malfunctions.