0

I'm trying to get the text in my textbox tb_key to write to my std::string Key Variable by doing this:

std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;

I get an error saying :

A non-static member reference must be relative to a specific object

I tried to search but I couldn't find anything really that fixed it for me.

Minimal Reproducible Example:

LoginForm.h

namespace TRIPRECOILWARE {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;



private: System::Void tb_key_TextChanged(System::Object^ sender, System::EventArgs^ e) 
{
}

}

LoginForm.cpp

std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;

I'm trying to use this in LoginForm.h

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    
    if (Authenticate(StringToChar(Key), (StringToChar(hwid)))) // Authenticates key & hwid
    {
        this->Hide();
        Software^ soft = gcnew Software();
        soft->Show();
    }

Basically, I want to get Key from Textbox called tb_key and write it to my Key variable defined above. Then use that key to authenticate and perform code

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
TRAV
  • 3
  • 2
  • 1
    You mean `String^ sKey = this->tb_key->Text;`? Assuming LoginForm is the current class. But you can also use `MyNameSpace::LoginForm::tb_key->Text` form anyway. – Jimi Apr 07 '21 at 19:46
  • Using `MyNameSpace::LoginForm::tb_key->Text` gives me the error mentioned. I'm defining the Variable in my .cpp file not in my .h original Form file. – TRAV Apr 07 '21 at 19:53
  • Please show a [mre]. Presumably `LoginForm` is a class not an object – Alan Birtles Apr 07 '21 at 20:02
  • Then you don't have the Instance of LoginForm, so `MyNameSpace::LoginForm` represents a Type, while `tb_key` is an Instance member. You need a reference to the Instance of the Form you have created. Is this the `MyNameSpace.cpp` file? – Jimi Apr 07 '21 at 20:14
  • I renamed MyNameSpace in the code above which is TRIPRECOILWARE So LoginForm is the .h and .cpp file. – TRAV Apr 07 '21 at 20:15
  • Just forget about "Key" entirely and write `Authenticate(StringToChar(tb_key->Text))` – Ben Voigt Apr 07 '21 at 20:18
  • You seem to be under the impression that a variable definition creates a permanent connection between the new variable (`Key`) and the initializer expression `LoginForm::tb_key->Text`. But that is not how variable initialization works. Rather, it evaluates the expression (once) to become a value, copies that value to the variable, and never looks at the initializer again. The value in the variable never updates unless you do something to update it. – Ben Voigt Apr 07 '21 at 20:20
  • Did that and Error: No suitable constructor exists to convert from "System::String ^" to "std::basic _string, std::allocator>" – TRAV Apr 07 '21 at 20:22
  • Add to that a confused mix of standard C++ string and .NET System::String types, trying to access a variable before it is declared, order of operations.... you are better off just cutting out the middleman. – Ben Voigt Apr 07 '21 at 20:22
  • Yeah, that error concerns your confused mix of standard C++ string and .NET System::String. What is `StringToChar` supposed to do? – Ben Voigt Apr 07 '21 at 20:23
  • Here is the code for my `StringToChar` ``` char* StringToChar(std::string string) //A function to convert a string to a char { return _strdup(string.c_str()); } ``` – TRAV Apr 07 '21 at 20:24
  • That function is absolutely useless inside of a .NET program. What argument types does `Authenticate()` expect? Is it calling `std::free()` on its arguments? – Ben Voigt Apr 07 '21 at 20:25
  • bool Authenticate(const char *license, const charhwid="") – TRAV Apr 07 '21 at 20:27

1 Answers1

0

Your real problem is a duplicate of How to turn System::String^ into std::string?

Corrected code:

#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

using namespace System;
using namespace msclr::interop;

void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
    std::string Key = marshal_as<std::string>(tb_key->Text);
    if (Authenticate(Key.c_str(), hwid.c_str())) // Authenticates key & hwid
    {
        Hide();
        Software^ soft = gcnew Software();
        soft->Show();
    }
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thank you so much! I got this error tho: Severity Code Description Project File Line Suppression State Error C4996 'msclr::interop::error_reporting_helper<_To_Type,_From_Type,false>::marshal_as': This conversion is not supported by the library or the header file needed for this conversion is not included. Please refer to the documentation on 'How to: Extend the Marshaling Library' for adding your own marshaling method. TRIPRECOILWARE C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\msclr\marshal.h 216 – TRAV Apr 07 '21 at 20:44
  • @TRAV: Sorry, I had missed one of the two required header files. The error message was exactly correct "the header file needed for this conversion is not included". I've added it to my answer now. – Ben Voigt Apr 07 '21 at 20:46
  • The list of which header file is needed for which conversions can be found here: https://learn.microsoft.com/en-us/cpp/dotnet/overview-of-marshaling-in-cpp?view=msvc-160 – Ben Voigt Apr 07 '21 at 20:47
  • Thank it fixed it! I'm facing other errors now LNK2028 and LNK 2019 Severity Code Description Project File Line Suppression State Error LNK2028 unresolved token (0A00050D) "extern "C" bool __cdecl Initialize(char const *)" (?Initialize@@$$J0YA_NPBD@Z) referenced in function "int __clrcall main(cli::array^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z) – TRAV Apr 07 '21 at 20:49
  • It's coming from Initialize("245t8rtZneogWSMsfMsvOPW6UK7yw62DADWjqLWX935rDzIp2K1LeWj4oP4EJhDF"); Which expects const char – TRAV Apr 07 '21 at 20:50
  • That seems completely unrelated to this question. Make sure you have that `Initialize()` function defined somewhere, or if it is provided by a library make sure you link with the library. It seems weird that the name is mangled with all the parameter types even when you have used `extern "C"` – Ben Voigt Apr 07 '21 at 21:15