与“ LPCTSTR”类型的参数不兼容

时间:2018-10-24 05:23:03

标签: c++-cli

所以我得到了这个错误:

  类型为System::String ^

参数与的参数不兼容   输入LPCTSTR

当我尝试使用此代码时:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    String^ currentDesktop = System::Environment::GetEnvironmentVariable("USERPROFILE") + "\\Desktop\\Test";
    CreateDirectory (currentDesktop, NULL);
    String^ value = (this->listBox1)->Text;
    MessageBox::Show ("File has been created successfully. You've choosen: " + value, "Success", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
};

所以我不确切知道问题出在哪里,所以请帮助我。

1 个答案:

答案 0 :(得分:0)

谢谢你们,谢谢@Ben Voigt

此代码可以正常工作:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    String^ currentDesktop = System::Environment::GetEnvironmentVariable("USERPROFILE") + "\\Desktop\\Test";
    msclr::interop::marshal_context context;
    LPCTSTR desktop = context.marshal_as<LPCTSTR>(currentDesktop);
    CreateDirectory (desktop, NULL);
    String^ value = (this->listBox1)->Text;
    MessageBox::Show ("File has been created successfully. You've choosen: " + value, "Success", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
};