调用API WINSPOOL问题GetJOB
我写一个Visual C++程序获取打印作业的详细信息。 代码如下所示:调用API WINSPOOL问题GetJOB
HANDLE hPrinter;
DWORD needed, returned, byteUsed,level;
JOB_INFO_2 *pJobStorage1=NULL;
level = 2;
GetJob(hPrinter, jobId, level, NULL, 0, &needed);
if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
cout << "GetJobs failed with error code"
<< GetLastError() << endl;
pJobStorage1 = (JOB_INFO_2 *)malloc(needed);
ZeroMemory(pJobStorage1, needed);
cout << GetJob(hPrinter, jobId, level, (LPBYTE)pJobStorage1, needed, (LPDWORD)&byteUsed) << endl;
cout << pJobStorage1[0].pPrinterName<<endl;
按照documentation,pJobStorage1的输出不是数组,但是,当我更改IDE报错
pJobStorage1[0].pPrinterName
到
pJobStorage1.pPrinterName
所以,我想知道发生了什么。
你有一个指针。只要看到声明JOB_INFO_2 *pJobStorage1=NULL
随着pJobStorage1[0].pPrinterName
和pJobStorage1->pPrinterName
你访问第一个元素pJobStorage1 ist指向。
pJobStorage1.pPrinterName
是无效的,因为pJobStorage1
,你不能用一个指针去除一个指针。运营商。
如果你定义数组JOB_INFO_2 foo[5]
。 foo本身就是指向第一个数组元素的地址/指针。所以你可以使用foo而不是JOB_INFO_2 *
。所以,如果你有一个数组名称,你可以使用它而不是一个指针。一个指针也定义了一个特定类型的元素的地址。您可以将其用作数组。
指针和数组具有可互换的使用。
struct A
{
int a;
};
void Foo()
{
A a[3], *pa;
pa = a;
A b;
// Accessing first element
b = *a;
b = a[0];
b = pa[0];
b = *pa;
// Accessing second element
b = a[1];
b = pa[1];
b = *(pa+1);
// accessing member in first element.
int c;
c = a[0].a;
c = pa[0].a;
c = pa->a;
c = (*pa).a;
}
是否可以使用的对象?运营商只能访问会员? 对于指针,我可以使用 - >运算符来访问成员只? –
不可以。您可以取消指针并使用该指针。改变了我的答案,看最后一行。 – xMRi
它不是一个数组。你将它正确地声明为一个指针,并且做了内存管理权(不要忘记free(),让我们不要对新的vs malloc进行挑剔),你只需要' - >'来解引用它。荷兰荷兰。 –