gpt4 book ai didi

C++ 与 Dll 通信出错

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:02 24 4
gpt4 key购买 nike

我当前的项目有 2 个 dll,一个称为 Engine,另一个称为 Graphics。引擎链接到图形。然后是带有两者链接的主程序,但使用引擎。

在一些函数中是这样的:

typedef const char* ce_safechar;
AssimpIntegration::GetData(ce_safechar pFile, ...

此错误弹出:_pFirstBlock == pHead,就在我退出函数之后。

我见过很多不同的问题,似乎都指向字符串。

我怎样才能避免这种行为?

完整功能(如果需要):

FMESH_DATA AssimpIntegration::GetData(ce_safechar pFile, bool convLeftHanded, int maxSM)
{
// Create an instance of the Importer class
Assimp::Importer importer;
// And have it read the given file with some example post processing
// Usually - if speed is not the most important aspect for you - you'll
// probably to request more post processing than we do in this example.

int cls = 0x0;
if (convLeftHanded)
cls = aiProcess_ConvertToLeftHanded;

const aiScene* scene = importer.ReadFile( pFile, aiProcess_GenNormals | aiProcess_FindInstances | aiProcess_CalcTangentSpace | cls | aiProcess_Triangulate );

// If the import failed, report it
if (!scene)
{
char* error = (char*)importer.GetErrorString();
CE_ERROR(error, "Assimp Error");
}

// Now we can access the file's contents.
Group<MESH_STRUCT> Vertices;
Group<DWORD> indices;

//////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////PROCESS MESH///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

FMESH_DATA data;

if (scene->HasAnimations())
{
aiAnimation *anim = scene->mAnimations[0];

FOREACH(anim->mChannels[0]->mNumPositionKeys)
{
aiVector3D position = anim->mChannels[0]->mPositionKeys[i].mValue;

CE_ANIMATIONKEY key;
key.position = D3DXVECTOR3(position.x, position.y, position.z);

data.Keys.push_back(key);
}
}

D3DXVECTOR3 norms;
float tanx, tany, tanz;
float bitanx, bitany, bitanz;

FOREACH (scene->mNumMeshes)
{
if (i >= maxSM)
continue;

aiMesh *mesh = scene->mMeshes[i];
Vertices.group.clear();
indices.group.clear(); //dafuck?

if (mesh->HasPositions())
{
for (int v = 0; v != mesh->mNumVertices; v++)
{
norms = D3DXVECTOR3(0,0,0);
if (mesh->HasNormals())
norms = D3DXVECTOR3(mesh->mNormals[v].x,mesh->mNormals[v].y,mesh->mNormals[v].z);

tanx = tany = tanz = 0;
bitanx = bitany = bitanz = 0;

if (mesh->HasTangentsAndBitangents())
{
tanx = mesh->mTangents[v].x; tany = mesh->mTangents[v].y; tanz = mesh->mTangents[v].z;
bitanx = mesh->mBitangents[v].x; bitany = mesh->mBitangents[v].y; bitanz = mesh->mBitangents[v].z;
}

Vertices.push_back(MESH_STRUCT(
mesh->mVertices[v].x,
mesh->mVertices[v].y,
mesh->mVertices[v].z,
norms,
0,
0,
tanx, // TANGENTS
tany,
tanz
));

for (int b = 0; b < mesh->mNumBones; b++)
{
if (b > 4)
break;

float weight = 0.0f;

for ( int w = 0; w < mesh->mBones[b]->mNumWeights; w++)
if (mesh->mBones[b]->mWeights[w].mVertexId == v)
weight = mesh->mBones[b]->mWeights[w].mWeight;

Vertices.back().bWeight[b] = weight;
}

if (mesh->HasTextureCoords(0))
{
Vertices.back().U = mesh->mTextureCoords[0][v].x;
Vertices.back().V = mesh->mTextureCoords[0][v].y;
}
}

for (int f = 0; f != mesh->mNumFaces; f++)
{
for (int index = 0; index != mesh->mFaces[f].mNumIndices; index++)
{
indices.push_back(mesh->mFaces[f].mIndices[index]);
}
}
}

data.meshData.push_back(Vertices);
data.indices.push_back(indices);

// Set the required textures
const aiMaterial* pMaterial = scene->mMaterials[mesh->mMaterialIndex];

if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
aiString Path;

if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
std::string FullPath = Path.data;

if (FullPath.find("\\") != string::npos)
FullPath = FullPath.substr(FullPath.find_last_of("\\")+1, FullPath.length() - (FullPath.length() - FullPath.find_last_of("\\")-1));
else if (FullPath.find("/") != string::npos)
FullPath = FullPath.substr(FullPath.find_last_of("/")+1, FullPath.length() - FullPath.find_last_of("/")-1);

string rFile = pFile;
string spFile = std::string(pFile);

if (spFile.find("\\") != string::npos)
rFile = spFile.substr(0, spFile.find_last_of("\\")+1);
else
rFile = spFile.substr(0, spFile.find_last_of("/")+1);

FullPath = rFile + FullPath;

data.textures.push_back(FullPath);
}
}
else
{
data.textures.push_back("");
}
}

data.scene = scene;
return data;
}

最佳答案

您正在使用 STL 对象作为您在 DLL 上导出的函数/方法的参数和/或返回类型。

示例:您返回 FMESH_DATA。这个类/结构内部有 STL 对象,我们可以通过以下行看到:

data.Keys.push_back(key);

这里的问题是:通过 DLL 导出或导入 STL 内容不是一个好主意。它要求 DLL 的调用者使用在 DLL 中创建的对象的相同 CRT。

但如果您真的想这样做,您可以将项目(所有项目)的运行时库更改为多线程 (/MD)。然后您将能够跨客户端和 DLL 安全地使用 STL。

关于C++ 与 Dll 通信出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16927709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com