我一直在编写一个需要2.6或更高版本的wine的程序。我想把它作为布尔值,所以我一直在尝试使用下面的代码:
#include <string>
#include <iostream>
#include "WineCheck.h"
#include <cstdlib>
using namespace std;
bool checkForWine()
{
// Create variables for checking wine state
bool wineIsThere = false;
bool wineIsVersion = false;
// Check dpkg if wine is there
if (string(system("dpkg -l | cut -c 4-9 | grep \\ wine\\ ")) == " wine ")
{
wineIsThere = true;
// Check version
if (double(system("wine --version | cut -c 6-8")) >= 2.6)
wineIsVersion = true;
}
// Return
if (wineIsThere && wineIsVersion)
return true;
else
return false;
}发布于 2014-11-06 09:50:16
首先,我认为你不应该为此而烦恼。Wine 2.6应该只作为依赖项包含在您的配置脚本和/或包文件中。如果您想保持到其他不使用该打包程序的GNU/Linux发行版的可移植性,那么在程序源代码中以特定的包管理系统为目标不是一个好主意。
来回答你的问题。我发现有两种方法可以做到这一点。你可以查看/var/lib/dpkg/status。Read through the file line by line,直到您看到这一部分。如果您没有找到该部分,或者Status: ...行没有显示installed,那么就没有安装wine。Version: ...行将告诉您所安装的是哪个版本。我通过在Debian Wheezy上安装和卸载Wine来验证这种方法的有效性。您没有说您正在使用哪个发行版,但很明显您使用的是Debian打包系统,所以这应该也适用于Ubuntu和其他基于Debian的发行版。
$cat /var/lib/dpkg/status
...
Package: wine
Status: install ok installed
Priority: optional
Section: otherosf
Installed-Size: 80
Maintainer: Debian Wine Party <pkg-wine-party@lists.alioth.debian.org>
Architecture: amd64
Version: 1.4.1-4
...另一个选项是使用libdpkg。我找到了一个example that lists all installed packages。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-array.h>
#include "filesdb.h"
const char thisname[] = "example1";
int
main(int argc, const char *const *argv)
{
struct pkg_array array;
struct pkginfo *pkg;
int i;
enum modstatdb_rw msdb_status;
standard_startup();
filesdbinit();
msdb_status = modstatdb_open(msdbrw_readonly);
pkg_infodb_init(msdb_status);
pkg_array_init_from_db(&array);
pkg_array_sort(&array, pkg_sorter_by_name);
for (i = 0; i < array.n_pkgs; i++) {
pkg = array.pkgs[i];
if (pkg->status == stat_notinstalled)
continue;
printf("Package --> %s\n", pkg->set->name);
}
pkg_array_destroy(&array);
modstatdb_shutdown();
standard_shutdown();
}https://stackoverflow.com/questions/26770155
复制相似问题