我要把ShellExecute(p->m_hWnd,"open","dd.exe","","", SW_HIDE)中的返回值转换为错误信息改怎么办?

ctdc1980 2004-03-31 03:58:36
我要把ShellExecute(p->m_hWnd,"open","dd.exe","","", SW_HIDE)中的返回值转换为错误信息改怎么办?谢谢!
...全文
203 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
kvw3000 2004-08-10
  • 打赏
  • 举报
回复
获取最后一次错误信息文本内容:
char* LastErrMsgFormat(char* ErrBuf)
{
LPVOID lpMsgBuf;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
// MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // chinese msg
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), // english msg
(LPTSTR) &lpMsgBuf,
0,
NULL
);
sprintf(ErrBuf, (LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );

return ErrBuf;
}

dropingleaf 2004-08-10
  • 打赏
  • 举报
回复
不同返回值的意义!
0 操作系统内存或资源不够
ERROR_FILE_NOT_FOUND 文件没找到
ERROR_PATH_NOT_FOUND 指定路径不对
ERROR_BAD_FORMAT 不是合法的应用程序
SE_ERR_ACCESSDENIED 操作系统拒绝访问此文件
SE_ERR_ASSOCINCOMPLETE 文件名关联不完整或非法
SE_ERR_DDEBUSY 不能完成DDE事务,因为正在处理其它DDE事务  
SE_ERR_DDEFAIL DDE事务失败
SE_ERR_DDETIMEOUT 请示超时
SE_ERR_DLLNOTFOUND 没找到指定的动态链接库
SE_ERR_FNF 指定文件没找到。
SE_ERR_NOASSOC 没有与当前文件类型关联的应用程序
SE_ERR_OOM 内存不足
SE_ERR_PNF 路径没找到
SE_ERR_SHARE 共享错误
判断一下就可以了!
薛定谔之死猫 2004-04-01
  • 打赏
  • 举报
回复
楼主只有自己动手了
ctdc1980 2004-04-01
  • 打赏
  • 举报
回复
用GetLastError的得到的错误信息不具体,只是说“找不到指定的文件“
怎样才能得到更具题的信息比如是找不到这个dd.exe
薛定谔之死猫 2004-04-01
  • 打赏
  • 举报
回复
我找到的定义:
#ifdef STRICT
typedef void *HANDLE;
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif

DECLARE_HANDLE(HINSTANCE);
薛定谔之死猫 2004-04-01
  • 打赏
  • 举报
回复
纠正:
if(32<=ShellExecute(p->m_hWnd,"open","dd.exe","","", SW_HIDE))
...Error
薛定谔之死猫 2004-04-01
  • 打赏
  • 举报
回复
直接比啊
if(32<ShellExecute(p->m_hWnd,"open","dd.exe","","", SW_HIDE))
...Error
captainwh 2004-04-01
  • 打赏
  • 举报
回复
什么叫怎么比较??
int ErrCode = (int)res;
char szErr[255];
switch (ErrCode)
{
case ERROR_FILE_NOT_FOUND:
spritnf(szErr, "File Not Found");
break;
case ERROR_PATH_NOT_FOUND:
sprintf(szErr, "Path Not Found");
break:
...
};
ctdc1980 2004-04-01
  • 打赏
  • 举报
回复
Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwise.
那我怎么比较它的返回值是不是大于32呢?
薛定谔之死猫 2004-03-31
  • 打赏
  • 举报
回复
Example
//example for CFile::Open
CFile f;
CFileException e;
char* pFileName = "test.dat";
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
}

//A second example for CFile::Open.
//This console program uses CFile to copy binary files.

#include <afx.h>
#include <afxwin.h>
#include <iostream>

using namespace std;

CWinApp theApp;

int main(int argc, char *argv[])
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
{
cout << "panic: MFC couldn't initialize!" << endl;
return 1;
}

// constructing these file objects doesn't open them

CFile sourceFile;
CFile destFile;

// see that we have a reasonable number of arguments

if (argc != 3)
{
cout << "usage: " << argv[0];
cout << " <source> <dest>" << endl;
cout << endl;
return 1;
}

// we'll use a CFileException object to get error information

CFileException ex;

// open the source file for reading

if (!sourceFile.Open(argv[1],
CFile::modeRead | CFile::shareDenyWrite, &ex))
{
// complain if an error happened
// no need to delete the ex object

TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
cout << "Couldn't open source file: ";
cout << szError;
return 1;
}
else
{
if (!destFile.Open(argv[2], CFile::modeWrite |
CFile::shareExclusive | CFile::modeCreate, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
cout << "Couldn't open source file: ";
cout << szError;

sourceFile.Close();
return 1;
}

BYTE buffer[4096];
DWORD dwRead;

// Read in 4096-byte blocks,
// remember how many bytes were actually read,
// and try to write that many out. This loop ends
// when there are no more bytes to read.

do
{
dwRead = sourceFile.Read(buffer, 4096);
destFile.Write(buffer, dwRead);
}
while (dwRead > 0);

// Close both files

destFile.Close();
sourceFile.Close();
}

return 0;
}
薛定谔之死猫 2004-03-31
  • 打赏
  • 举报
回复
相信你看过:
Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwise. The following table lists the error values. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. The only thing that can be done with the returned HINSTANCE is to cast it to an int and compare it with the value 32 or one of the error codes below.

0 The operating system is out of memory or resources.
ERROR_FILE_NOT_FOUND The specified file was not found.
ERROR_PATH_NOT_FOUND The specified path was not found.
ERROR_BAD_FORMAT The .exe file is invalid (non-Microsoft Win32® .exe or error in .exe image).
SE_ERR_ACCESSDENIED The operating system denied access to the specified file.
SE_ERR_ASSOCINCOMPLETE The file name association is incomplete or invalid.
SE_ERR_DDEBUSY The Dynamic Data Exchange (DDE) transaction could not be completed because other DDE transactions were being processed.
SE_ERR_DDEFAIL The DDE transaction failed.
SE_ERR_DDETIMEOUT The DDE transaction could not be completed because the request timed out.
SE_ERR_DLLNOTFOUND The specified dynamic-link library (DLL) was not found.
SE_ERR_FNF The specified file was not found.
SE_ERR_NOASSOC There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable.
SE_ERR_OOM There was not enough memory to complete the operation.
SE_ERR_PNF The specified path was not found.
SE_ERR_SHARE A sharing violation occurred.

如果真的想把返回错误做成文本消息,就用多分支处理返回值好了(用消息框最简单)。
thememory 2004-03-31
  • 打赏
  • 举报
回复
ShellExecute(p->m_hWnd,"open","dd.exe","","", SW_HIDE);
之后直接调用GetLastError就可以了,不需要比较
ctdc1980 2004-03-31
  • 打赏
  • 举报
回复
大侠,我知道msdn上有一段:
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0, // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
但是ShellExecute的返回值是HINSTANCE类型我怎么判断,比较。谢谢
薛定谔之死猫 2004-03-31
  • 打赏
  • 举报
回复
看看MSDN,错误处理无非有那么几种,直接看返回值,*GetLastError,异常等。
ctdc1980 2004-03-31
  • 打赏
  • 举报
回复
还有比如 CFile file ;
file.Open("state.txt",CFile::modeWrite);文件不存在的话
怎么获取错误信息?

16,472

社区成员

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

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

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