请看一个EnumResourceNames函数调用中的错误

onlynight 2002-05-30 10:34:23
在对话框中调用
EnumResourceNames( hInstance, RT_MENU, MyEnumProcedure, (LONG)hWnd )
其中 HINSTANCE hInstance
BOOL CALLBACK MyEnumProcedure(HANDLE hModule,LPCTSTR lpszType,
LPTSTR lpszName, LONG lParam)

编译时出现如下错误(只有这一个错误):
error C2664: 'EnumResourceNamesA' : cannot convert parameter 3 from 'int (void *,const char *,char *,long)' to 'int (__stdcall *)(struct HINSTANCE__ *,const char *,char *,long)'
None of the functions with this name in scope match the target type


请教
为什么会出现EnumResourceNamesA,是函数别名?

在MSDN中查EnumResourceNames函数 得到如下定义:
BOOL EnumResourceNames(
HINSTANCE hModule, // resource-module handling
LPCTSTR lpszType, // pointer to resource type
ENUMRESNAMEPROC lpEnumFunc, // pointer to callback function
LONG lParam // application-defined parameter
);
好像我写的没有错误啊?
该如何解决?
谢谢
...全文
114 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
onlynight 2002-05-31
  • 打赏
  • 举报
回复
十分感谢两位的帮助
问题解决了
我把函数改为static的
对于list的修改我使用的是SendDlgItemMessage函数

to xuying():MyEnumProcedure函数在实现部分 好像不用在头上加static


为了表示对两位的谢意
我已经另开一贴给两位再各加50分(现在每题最多还是只能100分^_^)
loyee 2002-05-31
  • 打赏
  • 举报
回复
如果CGetResDlg不是你的主窗口,你就要用其它方法取得类句柄代替上面((CGetResDlg *)AfxGetMainWnd())的方法.
loyee 2002-05-31
  • 打赏
  • 举报
回复
onlynight(歧异) ,

1)CGetResDlg类定义文件中改变函数定义.

static BOOL CALLBACK MyEnumProcedure(HANDLE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG lParam)

2)在
BOOL CALLBACK CGetResDlg::MyEnumProcedure(HANDLE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG lParam)
的最后

// Add it to the listbox
下面所有访问类成员变量m_list改成((CGetResDlg *)AfxGetMainWnd())->m_list

应该可以解决问题.
xuying 2002-05-31
  • 打赏
  • 举报
回复
加在最前面就可以了。
class CGetResDlg {
......
static BOOL CALLBACK MyEnumProcedure(......);
......
}

static BOOL CALLBACK CGetResDlg::MyEnumProcedure(HANDLE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG lParam)
{
......
}

不过我建议还是定义成全局函数比较好。
xuying 2002-05-31
  • 打赏
  • 举报
回复
果然如 loyee() 所说,定义在类里了。
BOOL CALLBACK CGetResDlg::MyEnumProcedure(HANDLE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG lParam)

MyEnumProcedure()这个函数最好定义成全局函数,如果作为类成员函数,应该加上static修饰。
onlynight 2002-05-31
  • 打赏
  • 举报
回复
STATIC应该怎么加
我对STATIC总是不太熟练
onlynight 2002-05-31
  • 打赏
  • 举报
回复
以下是其中一段用来获取ICON的,将ICON信息放入list控件
因为对于获取ICON这一段中的一些参数设定我是最没有把握的,其它的资源获取相对要简单
不知错误出在哪里 再谢

其中AddItemToList的传入参数hInstance = LoadLibraryEx( szFileName, NULL, LOAD_LIBRARY_AS_DATAFILE ))
//
//called before getting items from the list
//
BOOL CGetResDlg::AddItemToList(HINSTANCE hInstance)
{
// Fill in the listbox with the icons available
HWND hWnd=::GetActiveWindow();
if(!EnumResourceNames(hInstance,
RT_GROUP_ICON,
MyEnumProcedure,
(LONG)hWnd) )

{
return FALSE;
}
return TRUE;
}

BOOL CALLBACK CGetResDlg::MyEnumProcedure(HANDLE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG lParam)
{
LONG nIndex = LB_ERR;
DWORD lpID;
CString strItemName;

// Name is from MAKEINTRESOURCE()
if( HIWORD(lpszName) == 0 )
{
strItemName.Format("Icon [%s]", (DWORD)lpszName);
lpID=(DWORD)lpszName;
}
else
{
// Name is string
lpID =(DWORD)atol((CString)lpszName);
strItemName.Format("Icon [%s]", lpszName);
}

// Add it to the listbox
nIndex=m_list.AddString(strItemName);

// Set the item data to be the name of the resource so we can
//get it later
m_list.SetItemData(nIndex,lpID);

return TRUE;
}
loyee 2002-05-31
  • 打赏
  • 举报
回复
xuying() ,

呵呵,我怀疑他把CALLBACK函数定义在类里面没有加STATIC,模拟后的错误跟他描述的一样.
xuying 2002-05-31
  • 打赏
  • 举报
回复
你从哪里看出是成员函数了?
loyee 2002-05-31
  • 打赏
  • 举报
回复
哦,我发现他把
BOOL CALLBACK MyEnumProcedure(HANDLE hModule,LPCTSTR lpszType,
LPTSTR lpszName, LONG lParam)

定义在成员函数了,应该定义成STATIC或者全局函数就没问题了.
loyee 2002-05-31
  • 打赏
  • 举报
回复
onlynight(歧异),

我试了没有问题的啊.

如果用EnumResourceNames( hInstance, RT_MENU,MyEnumProcedure, (LONG)hWnd )

是有问题,错误提示跟你说的一样.

但是改成
EnumResourceNames( hInstance, RT_MENU,(ENUMRESNAMEPROC)MyEnumProcedure, (LONG)hWnd )就没有问题了.



xuying 2002-05-30
  • 打赏
  • 举报
回复
帖出你改了的代码和出错信息来看看。
onlynight 2002-05-30
  • 打赏
  • 举报
回复
多谢两位
我是加了CALLBACK的
不过对于那个错误仍是一筹莫展
拼写是可以保证没有错误的


xuying 2002-05-30
  • 打赏
  • 举报
回复
原来如此。
loyee 2002-05-30
  • 打赏
  • 举报
回复
xuying() ,

MSDN例子的编译器设置默认所有函数是__stdcall类型的.所以不加CALLBACK可以通过编译.从上位老兄的错误中应该是函数的定义问题.如果他在编译设置中不用默认设置.就可能会有问题.
xuying 2002-05-30
  • 打赏
  • 举报
回复
不需要加CALLBACK。msdn的例子里就没有加。
char szBuffer[80]; // print buffer for EnumResourceTypes
DWORD cbWritten; // number of bytes written to res. info. file
int cbString; // length of string in sprintf

// Declare callback functions.
BOOL EnumTypesFunc(
HANDLE hModule,
LPTSTR lpType,
LONG lParam);

BOOL EnumNamesFunc(
HANDLE hModule,
LPCTSTR lpType,
LPTSTR lpName,
LONG lParam);

BOOL EnumLangsFunc(
HANDLE hModule,
LPCTSTR lpType,
LPCTSTR lpName,
WORD wLang,
LONG lParam);

// Load the .EXE whose resources you want to list.
hExe = LoadLibrary("hand.exe");
if (hExe == NULL)
{
ErrorHandler("Could not load .EXE.");
}

// Create a file to contain the resource info.
hFile = CreateFile("resinfo.txt", // name of file
GENERIC_READ | GENERIC_WRITE, // access mode
0, // share mode
(LPSECURITY_ATTRIBUTES) NULL, // no security
CREATE_ALWAYS, // create flags
FILE_ATTRIBUTE_NORMAL, // file attributes
(HANDLE) NULL); // no template
if (hFile == INVALID_HANDLE_VALUE) {
ErrorHandler("Could not open file.");
}

// Find all of the loaded file's resources.
cbString = sprintf(szBuffer,
"The file contains the following resources:\n\n");
WriteFile(hFile, // file to hold resource info.
szBuffer, // what to write to the file
(DWORD) cbString, // number of bytes in szBuffer
&cbWritten, // number of bytes written
NULL); // no overlapped I/O

EnumResourceTypes(hExe, // module handle
(ENUMRESTYPEPROC)EnumTypesFunc, // callback function
0); // extra parameter
// Unload the executable file whose resources were
// enumerated and close the file created to contain
// the resource information.
FreeLibrary(hExe);
CloseHandle(hFile);

// FUNCTION: EnumTypesFunc(HANDLE, LPSTR, LONG)
//
// PURPOSE: Resource type callback
BOOL EnumTypesFunc(
HANDLE hModule, // module handle
LPTSTR lpType, // address of resource type
LONG lParam) // extra parameter, could be
// used for error checking
{
int cbString;
// Write the resource type to a resource information file.
// The type may be a string or an unsigned decimal
// integer, so test before printing.
if ((ULONG)lpType & 0xFFFF0000)
{
cbString = sprintf(szBuffer, "Type: %s\n", lpType);
}
else
{
cbString = sprintf(szBuffer, "Type: %u\n", (USHORT)lpType);
}

WriteFile(hFile, szBuffer, (DWORD) cbString,
&cbWritten, NULL);
// Find the names of all resources of type lpType.
EnumResourceNames(hModule,
lpType,
(ENUMRESNAMEPROC)EnumNamesFunc,
0);

return TRUE;
}

// FUNCTION: EnumNamesFunc(HANDLE, LPSTR, LPSTR, LONG)
//
// PURPOSE: Resource name callback
BOOL EnumNamesFunc(
HANDLE hModule, // module handle
LPCTSTR lpType, // address of resource type
LPTSTR lpName, // address of resource name
LONG lParam) // extra parameter, could be
// used for error checking
{
int cbString;
// Write the resource name to a resource information file.
// The name may be a string or an unsigned decimal
// integer, so test before printing.
if ((ULONG)lpName & 0xFFFF0000)
{
cbString = sprintf(szBuffer, "\tName: %s\n", lpName);
}
else
{
cbString = sprintf(szBuffer, "\tName: %u\n",
(USHORT)lpName);
}

WriteFile(hFile, szBuffer, (DWORD) cbString,
&cbWritten, NULL);
// Find the languages of all resources of type
// lpType and name lpName.
EnumResourceLanguages(hModule,
lpType,
lpName,
(ENUMRESLANGPROC)EnumLangsFunc,
0);

return TRUE;
}

// FUNCTION: EnumLangsFunc(HANDLE, LPSTR, LPSTR, WORD, LONG)
//
// PURPOSE: Resource language callback
BOOL EnumLangsFunc(
HANDLE hModule, // module handle
LPCTSTR lpType, // address of resource type
LPCTSTR lpName, // address of resource name
WORD wLang, // resource language
LONG lParam) // extra parameter, could be
// used for error checking
{
HANDLE hResInfo;
char szBuffer[80];
int cbString = 0;

hResInfo = FindResourceEx(hModule, lpType, lpName, wLang);
// Write the resource language to the resource information file.
cbString = sprintf(szBuffer, "\t\tLanguage: %u\n", (USHORT)wLang);
WriteFile(hFile, szBuffer, (DWORD) cbString,
&cbWritten, NULL);
// Write the resource handle and size to buffer.
cbString = sprintf(szBuffer,
"\t\thResInfo == %lx, Size == %lu\n\n",
hResInfo,
SizeofResource(hModule, hResInfo));
WriteFile(hFile, szBuffer, (DWORD) cbString,
&cbWritten, NULL);
return TRUE;
}
loyee 2002-05-30
  • 打赏
  • 举报
回复
xuying()改对一处,忘了原来对的地方. :-)

修改如下看看:
BOOL CALLBACK MyEnumProcedure(HANDLE hModule,LPCTSTR lpszType,
LPTSTR lpszName, LONG lParam)

EnumResourceNames( hInstance, RT_MENU, (ENUMRESNAMEPROC)MyEnumProcedure, (LONG)hWnd )
xuying 2002-05-30
  • 打赏
  • 举报
回复
你少写了一个参数吧,仔细检查一下,应该是你的输入错误。检查函数名是否对应。
onlynight 2002-05-30
  • 打赏
  • 举报
回复
纠正一个笔误
上面“error C2440: 'static_cast' : ”
实际是“error C2440: 'type cast' :”
onlynight 2002-05-30
  • 打赏
  • 举报
回复
如上修改后,出现如下错误:
error C2440: 'static_cast' : cannot convert from '' to 'int (__stdcall *)(struct HINSTANCE__ *,const char *,char *,long)'
None of the functions with this name in scope match the target type

继续请教
加载更多回复(1)

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧