LPCSTR 在 CLR 项目中创建未声明的变量

LPCSTR creates undeclared variable in CLR project

本文关键字:未声明 变量 创建 CLR 项目 LPCSTR      更新时间:2023-10-16

我正在尝试在dll包装器中创建一个c ++函数,该包装器采用char *,将其转换为LPCSTR,然后将其用作MessageBox中的变量。该函数和转换在原始类中工作正常,但是当我尝试在 CLR 项目中使用该函数时,使用 LPCSTR 变量出现未声明标识符错误。

空项目类标头:

#pragma once
#include <stdexcept>
using namespace std;
class StringPass {
public:
    void stringPass(char *inbound);
};

空项目类.cpp文件:

#pragma once
#include "NewPrototypes.h"
#include <Windows.h>
void StringPass::stringPass(char *inbound)
{
    LPCSTR inBound = inbound;
    MessageBox(NULL, TEXT(inBound), TEXT("Succesful Test"), MB_OK);
}

当我将其作为独立项目运行时,它可以完美运行。

CLR 项目标头:

#pragma once
#include "C:UsersRyanDocumentsVisual Studio 2010ProjectsNewProtoClassNewProtoClassNewPrototypes.h"
#include "C:UsersRyanDocumentsVisual Studio 2010ProjectsNewProtoClassNewProtoClassNewPrototypes.cpp"
using namespace System;
namespace NewPrototypesDll {
    public ref class StringPassWrapper
    {
    public:
        StringPassWrapper();
        void stringPassWrapper(char *inbound);
    private:
        StringPass *stringPassClass;
        // TODO: Add your methods for this class here.
    };
}

CLR 项目.cpp文件:

#include "stdafx.h"
#include "NewPrototypesDll.h"
#include "C:UsersRyanDocumentsVisual Studio 2010ProjectsNewProtoClassNewProtoClassNewPrototypes.h"
#include "C:UsersRyanDocumentsVisual Studio 2010ProjectsNewProtoClassNewProtoClassNewPrototypes.cpp"
NewPrototypesDll::StringPassWrapper::StringPassWrapper()
{
    stringPassClass = new StringPass();
}
void NewPrototypesDll::StringPassWrapper::stringPassWrapper(char *inbound)
{
    stringPassClass->stringPass(inbound);
}

当我尝试构建DLL文件时,出现错误:

1>C:UsersRyanDocumentsVisual Studio 2010ProjectsNewProtoClassNewProtoClassNewPrototypes.cpp(11): error C2065: 'LinBound' : undeclared identifier

我已经尝试了所有方法,我已经将 windows.h 添加到项目中,但此错误不断弹出。是的,我已经查看了有关未声明标识符错误的其他问题,但没有一个是我的情况所独有的,并且没有一个解决方案有效。为什么它在项目的第一部分有效,而在另一部分不起作用?

TEXT() 用于文字,而不是变量。请注意,错误消息显示"LinBound"——TEXT 在 Unicode 模式下在其参数的前面添加一个 L,而 CLR 处于 Unicode 模式。直接调用 MessageBoxA,或使用 MultiByteToWideChar 转换为 Unicode。