我是DLL世界的新手.我得到了一个Win32 DLL,它有很多功能.需要从C++调用这些DLL函数
我想调用CreateNewScanner
哪个创建一个新的扫描仪对象并在C++中获得结果.DLL中提到的函数是:
BOOL CreateNewScanner(NewScanner *newScan);
Run Code Online (Sandbox Code Playgroud)
和NewScanner
是一个struct
,如以下,
// Structure NewScanner is defined in "common.h" .
typedef struct{
BYTE host_no; // <- host_no =0
LONG time; // <- command timeout (in seconds)
BYTE status; // -> Host adapter status
HANDLE obj; // -> Object handle for the scanner
}NewScanner;
Run Code Online (Sandbox Code Playgroud)
我该怎么称呼这个功能?从C++开始,这是我管理的,
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
HINSTANCE hInstance;
if(!(hInstance=LoadLibrary("WinScanner.dll"))){
cout << "could not load library" << endl;
}
/* get pointer to …
Run Code Online (Sandbox Code Playgroud) 正如问题所述,我想将a的内容复制CStringArray
到一个std::vector<std::string>
.
有什么建议?
考虑类似的事情:
template <typename T>
void f(T& x)
{
....
}
Run Code Online (Sandbox Code Playgroud)
为什么喜欢绑定?const
int
f(T&)
这在我看来有点违反了const-correctness.实际上,如果f()
采用非 const T&
引用,则很可能f()
会修改其参数(否则,f()
将被定义为void f(const T&)
).
在这样的代码中:
template <typename T>
inline void f(T& x)
{
x = 0;
}
int main()
{
int n = 2;
f(n);
const int cn = 10;
f(cn);
}
Run Code Online (Sandbox Code Playgroud)
编译器试图调用f()
与T = const int
,那么当然有由于一个错误消息x = 0;
内分配f()
的身体.
这是来自GCC的错误消息:
Run Code Online (Sandbox Code Playgroud)test.cpp: In instantiation of …
我要带一个格式化的文本文件,并将不同的行放在随机位置的数组中。但是,当我尝试获取时rand() % 8
,我会获得以前的值。这使我丢失了一些行,并使数组的某些部分留空。
我应该如何修改它,以便以随机顺序获得从0到7的唯一值?我只使用我在课堂上学到的东西,导师不推荐新东西。请帮助我更改此代码的逻辑。
void get_input(string (&teams)[8][6]) {
srand(time(NULL));
string filename;
double value;
char buffer[100];
int diffnum[8];
string token;
cout << "Enter the input file: ";
cin >> filename;
ifstream infile;
infile.open (filename.c_str());
if (infile.is_open()) {
int teamcounter = 0;
while (infile.getline (buffer, 100) && teamcounter < 8) {
int ran = rand()%8;
for (int find = 0; find < 8; find++) {
while (diffnum[find] == ran) {
ran = rand()%8;
}
}
diffnum[teamcounter] = ran;
int counter …
Run Code Online (Sandbox Code Playgroud) 为了测量Windows上某些部分C++代码的执行时间,我倾向于使用QueryPerformanceCounter()
高分辨率计时器.这个例子可以在这篇关于STL性能的VCblog帖子中找到.
为了编写跨平台 C++代码,我可以将哪些函数/类用于同一目的?
当使用新的C++ 11时std::move()
,我注意到,由于ADL,它可以只写move()
,没有std::
命名空间前缀.
使用move()
得很好吗?有任何陷阱吗?它只是更好用std::move()
吗?
示例可编译代码如下:
#include <iostream>
#include <utility>
#include <vector>
template <typename T>
void print(const char* const descr, const std::vector<T>& v) {
std::cout << descr << ": ";
for (const auto& x : v) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
int main() {
std::vector<int> v{11, 22, 33};
std::vector<int> w = move(v);
print("v", v);
print("w", w);
}
Run Code Online (Sandbox Code Playgroud) #include<iostream>
using namespace std;
int main()
{
char a[2][2] = {"A","B"};
cout << a << endl;
}
Run Code Online (Sandbox Code Playgroud)
由于a[]
存储了第一个索引的地址,它必须打印A和B.但是它正在打印一些地址.打印A和B的程序有什么问题?
如果我input.size() - 1
用作for循环条件,程序将打印"进入循环" .
std::string input;
input = {""};
int i = 0;
for (; i < input.size() - 1; ++i)
{
cout << "Entered the loop" << endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将值传递input.size() -1
给整数(checksize
):
std::string input;
input = {""};
int checksize = input.size() - 1;
int i = 0;
for (; i < checksize; ++i)
{
cout << "Entered the loop" << endl;
}
Run Code Online (Sandbox Code Playgroud)
然后程序将不会进入循环并且不会打印"进入循环"
我想知道为什么会这样?看来这两段代码对我来说是一样的.
我有一些代码将各种元素组合到缓冲区中.我的代码看起来像这样:
static void CreatePacket(const std::string& source, const std::string id, const std::string payload, std::vector<char>& buffer)
{
buffer.resize(source.size() + id.size() + payload.size());
std::vector<char>::iterator bufferDest = buffer.begin();
// Start the message
char MessageStart = '$';
*bufferDest = MessageStart;
++bufferDest;
// Copy the message source
std::copy(source.begin(), source.end(), bufferDest);
bufferDest += source.size();
// Copy the message id
std::copy(id.begin(), id.end(), bufferDest);
bufferDest += id.size();
}
Run Code Online (Sandbox Code Playgroud)
该方法调用如下:
std::vector<char> buffer;
std::string source = "AB";
std::string id = "CDE";
std::string payload = "payload";
CreatePacket(source, id, payload, buffer);
Run Code Online (Sandbox Code Playgroud)
在std …
假设您要开发一个函数,在给定有效的注册表项句柄和值名称的情况下,如果该值存在于输入项下,则返回true,否则返回false,并在所有其他情况下抛出 C++ 异常。
bool RegValueExists(HKEY hKey, const std::wstring& value)
{
LRESULT retCode = ::RegGetValue(
hKey,
nullptr, // no subkey
value.c_str(),
RRF_RT_ANY, // any type
nullptr, nullptr, nullptr // not interested in these
);
Run Code Online (Sandbox Code Playgroud)
如果RegGetValue
成功,则返回0
;所以,在这种情况下,我可以返回true
给调用者。
但是,从的MSDN文档RegGetValue
,它并不清楚什么没有发现注册表值的情况下,API返回:
返回值
[...] 如果函数失败,返回值是系统错误代码。
在我的测试中,如果未找到值,则RegGetValue
返回2
(即ERROR_FILE_NOT_FOUND
)。但是,我找不到任何官方的MSDN 页面来记录这一点。(此外,因为注册表值什么时候是文件??)
在“系统错误代码”中还有一个ERROR_NOT_FOUND
代码 ( 1168
)。将其作为“未找到注册表值”的返回码是否合理?
我认为至少应该对 MSDN 中的常见错误代码进行清楚的解释。