gpt4 book ai didi

windows - 找不到函数 dllexport

转载 作者:可可西里 更新时间:2023-11-01 10:36:24 32 4
gpt4 key购买 nike

我有我的dll项目

// .h
#pragma once

#include <stdio.h>

extern "C"
{
void __declspec(dllexport) __stdcall sort(int* vect, int size);
}

//.cpp
#include "stdafx.h"

void __declspec(dllexport) __stdcall sort(int* vect, int size)
{

}

我有我的控制台项目:

#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>


/* Pointer to the sort function defined in the dll. */
typedef void (__stdcall *p_sort)(int*, int);


int _tmain(int argc, LPTSTR argv[])
{
p_sort sort;

HINSTANCE hGetProcIDDLL = LoadLibrary("dllproj.dll");

if (!hGetProcIDDLL)
{
printf("Could not load the dynamic library\n");
return EXIT_FAILURE;
}

sort = (p_sort)GetProcAddress(hGetProcIDDLL, "sort");

if (!sort)
{
FreeLibrary(hGetProcIDDLL);
printf("Could not locate the function %d\n", GetLastError());
return EXIT_FAILURE;
}

sort(NULL, 0);

return 0;
}

问题是我的函数 sort colud 找不到,即函数 GetProcAddress 总是返回 NULL

为什么?我该如何解决?

编辑:按照建议使用 __cdecl(在 dll 项目中而不是 __stdcall)和 Dependency Walker:

enter image description here

我也改变了以下(在我的主要部分),但它仍然不起作用。

typedef void (__cdecl *p_sort)(int*, int);

最佳答案

函数以修饰名导出。出于调试目的,当遇到这种情况时,使用 dumpbin或 Dependency Walker 来找出那个名字是什么。我预测它将是:_sort@8 . documentation对于 __stdcall调用约定给出装饰规则如下:

Name-decoration convention: An underscore (_) is prefixed to the name. The name is followed by the at sign (@) followed by the number of bytes (in decimal) in the argument list. Therefore, the function declared as int func( int a, double b ) is decorated as follows: _func@12

您必须执行以下操作之一:

  1. 导入时使用修饰名。
  2. 使用__cdecl避免装修。
  3. 使用 .def 文件导出以避免修饰。

关于windows - 找不到函数 dllexport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24138864/

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