M. *_* R. 6 c++ compilation visual-studio
我已经使用visual studio 2015开发了一个c ++项目.我的项目的输出是一个可执行文件,每个客户端必须有一个唯一的ID,并且该ID必须可以在代码中访问.一个简单的方法是在代码中定义一个常量变量,并为每个客户端更改其值并构建它多次,但我有一个Linux服务器,我不确定我是否可以构建它只是因为我使用了很多Winapi库.我在想,也许还有另一种方法可以改变输入或为输出添加一些常量值,就像操作可执行文件一样.例如:
#include <string>
#include <iostream>
#include <Windows.h>
const std::string ID = "some unique ID";
int main() {
std::cout << "Your ID: " << ID << std::endl;
getchar();
return(0);
}
Run Code Online (Sandbox Code Playgroud)
看来只有两种办法。一种是在 Linux 环境中构建项目,这是一种更好的方法,但必须使用一些工具,例如Mono XBuild 链接此处。另一个可能更简单的选项是打开二进制文件并操作特定的字符串。正如 @aloMalbarez 评论 这是一个基于此的简单脚本。假设这个例子:(我使用50m秒作为我的ID的固定长度)
#include <string>
#include <iostream>
#include <Windows.h>
#define ID "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
using namespace std;
int main() {
cout << "Your ID: " << ID << "\nlen:" << strlen(ID) << endl;
getchar();
return(0);
}
Run Code Online (Sandbox Code Playgroud)
生成可执行文件后,使用以下脚本创建输出。我不是 Linux 用户,所以你可以帮助我改进这一点。
./build.sh input.exe output.exe "myfixedID"
#!/bin/bash
# build.sh input_file output_file <ID>
input_file=$1
output_file=$2
ID=$3
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "wrong parameters"
echo "build.sh input_file output_file <ID>"
exit 1
fi
# use fixed string (ID) in the source file
# this creates 50 of "m"s
search_value=$(printf 'm%.0s' {1..50})
extension=".back"
temp_file="$input_file$extension"
tmpstring_file="./tmp"
null_termin='\0'
echo "copying the original file..."
yes | cp -rf $input_file $temp_file
address=$(strings -t d $temp_file | grep $search_value | grep -o '[0-9]*')
echo "Address:"
echo $address
if ! [[ $address =~ ^[0-9]+$ ]]; then
echo "cannot find valid ID in executable"
echo "removing temps"
rm $temp_file
exit 1
fi
# make the tempstring file
printf "$ID$null_termin" > $tmpstring_file
dd if=$tmpstring_file of=$temp_file obs=1 seek=$address conv=notrunc
echo "make new file"
yes | cp -rf $temp_file $output_file
echo "removing temps"
rm $temp_file $tmpstring_file
echo "Done!"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
384 次 |
| 最近记录: |