I want to retrive files with extension of .jpg from directory in vc++ 08. I'm not much comfortable with pointers so I got error when I try to get length of string in following code. Can anyone help me out??
Here is my code..
#include<iostream>
#include <string>
#pragma warning( disable : 4996 )
#include "dirent.h"
#pragma warning( default : 4996 )
using namespace std;
int main(int argc, char *argv[])
{
if(argc != 2)
{
cout<<"Usage: "<<argv[0]<<" <Directory name>"<<endl;
return -1;
}
DIR *dir;
dir = opendir (argv[1]);
if (dir != NULL)
{
string name; string ext = ".jpg"; int len;
cout<<"Directory Listing for "<<argv[1]<<" : "<<endl;
struct dirent *ent;
while ((ent = readdir (dir)) != NULL)
{
name = ent->d_name;
len = name.length; // here i got error...
name = name.substr(0, len-4);
if(_stricmp(name.c_str(), ext.c_str()) == 0)
{
cout<<ent->d_name<<endl;
}
else
{
cout<<"No JPG file"<<endl;
}
}
}
else
{
cout<<"Invalid Directory name"<<endl;
return -1;
}
return 0;
}
And error is
example.cpp(29) : error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::length': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::length' to create a pointer to member
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>\example.cpp(29) : error C2440: '=' : cannot convert from 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const' to 'int'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> There is no context in which this conversion is possible
Thanks in advance...
len = name.length();
// ^^
length is a member function, not a member variable. You need to call that function.
Related
I tried to use std::thread in Visual Studio 2013. It works all right like this.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <vector>
void thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::thread> threads;
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++) {
threads.emplace_back( std::thread(thread_task, i + 1));
}
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t : threads) {
t.join();
}
std::cout << "All threads joined.\n";
return 0;
}
but when I tried to make thread in a Class, I met some compiling errors.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <chrono> // std::chrono::seconds
#include <iostream> // std::cout
#include <thread> // std::thread, std::this_thread::sleep_for
#include <vector>
class MyClass
{
public:
MyClass();
void thread_task(int n);
~MyClass();
private:
};
MyClass::MyClass()
{
std::vector<std::thread> threads;
std::cout << "Spawning 5 threads...\n";
for (int i = 0; i < 5; i++) {
threads.emplace_back(std::thread(&MyClass::thread_task, i + 1));
}
std::cout << "Done spawning threads! Now wait for them to join\n";
for (auto& t : threads) {
t.join();
}
std::cout << "All threads joined.\n";
}
void MyClass::thread_task(int n) {
std::this_thread::sleep_for(std::chrono::seconds(n));
std::cout << "hello thread "
<< std::this_thread::get_id()
<< " paused " << n << " seconds" << std::endl;
}
MyClass::~MyClass(){}
int _tmain(int argc, _TCHAR* argv[])
{
MyClass test();
return 0;
}
The error messages are copied below.
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1149): error C2064: term does not evaluate to a function taking 1 arguments
1> class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::_Do_call<,0>(std::tuple<>,std::_Arg_idx<0>)' being compiled
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(1137) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::_Do_call<,0>(std::tuple<>,std::_Arg_idx<0>)' being compiled
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::operator ()<>(void)' being compiled
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(195) : see reference to function template instantiation 'void std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>::operator ()<>(void)' being compiled
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(192) : while compiling class template member function 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)'
1> with
1> [
1> _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(187) : see reference to function template instantiation 'unsigned int std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *)' being compiled
1> with
1> [
1> _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\thr\xthread(205) : see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1> [
1> _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\thread(49) : see reference to function template instantiation 'void std::_Launch<std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Target=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall MyClass::* )(int),void,MyClass,int>,int>
1> ]
1> d:\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(29) : see reference to function template instantiation 'std::thread::thread<void(__thiscall MyClass::* )(int),int>(_Fn &&,int &&)' being compiled
1> with
1> [
1> _Fn=void (__thiscall MyClass::* )(int)
1> ]
1>
What cause the problem?
Thanks #malchemist, when add this solve the problem. But it makes me confused this piece of codecode error still got some compiling errors even written just like the answer shows.
Can anyone help me? Thanks a lot.
You have to specify an object to call function inside new thread. Try this:
threads.emplace_back(std::thread(&MyClass::thread_task,this, i + 1));
I am using vs2012 and having the same problem. below is my sample source code.
#include "stdafx.h"
#include "iostream"
#include "list"
#include "algorithm"
#include "xutility"
using namespace std;
typedef struct Student{
string name;
int age;
int operator==(list<Student>::iterator itr)
{
return (!strcmp(itr->name.c_str(),this->name.c_str()));
}
}SStudent;
int _tmain(int argc, _TCHAR* argv[])
{
list<SStudent> myList;
SStudent temp1;
temp1.age = 10;
temp1.name = "aaaa";
myList.push_back(temp1);
list<SStudent>::iterator itr;
itr = std::find(myList.begin(), myList.end(), "aaaa");
return 0;
}
here is the error:
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Student' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3186
I'm trying to compile log4cpp in visual studio 2008 but I'm getting some errors like these:
Error 26 error C2664: 'ReportEventW' : cannot convert parameter 8 from 'const char *[1]' to 'LPCWSTR *' ...\testlog4cppdll\log4cpp\src\nteventlogappender.cpp 64
Error 19 error C2676: binary '+' : 'std::basic_string<_Elem,_Traits,_Ax>' does not define this operator or a conversion to a type acceptable to the predefined operator ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 11 error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : template parameter '_Elem' is ambiguous ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 10 error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 18 error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 16 error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 15 error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 7 error C2784: 'std::_Vb_const_iterator<_Sizet,_Difft,_MycontTy> std::operator +(_Difft,std::_Vb_const_iterator<_Sizet,_Difft,_MycontTy>)' : could not deduce template argument for 'std::_Vb_const_iterator<_Sizet,_Difft,_MycontTy>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 6 error C2784: 'std::_Vb_iterator<_Sizet,_Difft,_MycontTy> std::operator +(_Difft,std::_Vb_iterator<_Sizet,_Difft,_MycontTy>)' : could not deduce template argument for 'std::_Vb_iterator<_Sizet,_Difft,_MycontTy>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 9 error C2784: 'std::_Vector_const_iterator<_Ty,_Alloc> std::operator +(_Vector_const_iterator<_Ty,_Alloc>::difference_type,std::_Vector_const_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_const_iterator<_Ty,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 8 error C2784: 'std::_Vector_iterator<_Ty,_Alloc> std::operator +(_Vector_iterator<_Ty,_Alloc>::difference_type,std::_Vector_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_iterator<_Ty,_Alloc>' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 13 error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::basic_string<_Elem,_Traits,_Ax>' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 12 error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const _Elem,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 14 error C2784: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Error 17 error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'WCHAR [260]' ...\testlog4cppdll\log4cpp\src\dailyrollingfileappender.cpp 127
Here is the code:
hFind = FindFirstFile(LPTSTR(pattern.c_str()), &ffd); //Ahrost
if (hFind != INVALID_HANDLE_VALUE) {
do {
struct stat statBuf;
const std::string fullfilename = dirname + PATHDELIMITER + ffd.cFileName;
int res = ::stat(fullfilename.c_str(), &statBuf);
if (res != -1 && statBuf.st_mtime < oldest && !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
std::cout << "Deleting " << fullfilename << "\n";
::unlink(fullfilename.c_str());
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
// [XXX] some kind of error happened
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
The entire source of file is:
#include "PortabilityImpl.hh"
#ifdef LOG4CPP_HAVE_IO_H
# include <io.h>
#endif
#ifdef LOG4CPP_HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <log4cpp/DailyRollingFileAppender.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/FactoryParams.hh>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <time.h> //Ahrost
#ifdef LOG4CPP_HAVE_SSTREAM
#include <sstream>
#include <iomanip>
#endif
#ifndef WIN32 // only available on Win32
#include <dirent.h>
#endif
namespace log4cpp {
unsigned int DailyRollingFileAppender::maxDaysToKeepDefault = 30;
DailyRollingFileAppender::DailyRollingFileAppender(const std::string& name,
const std::string& fileName,
unsigned int maxDaysToKeep,
bool append,
mode_t mode) :
FileAppender(name, fileName, append, mode),
_maxDaysToKeep(maxDaysToKeep != 0 ? maxDaysToKeep : maxDaysToKeepDefault) {
struct stat statBuf;
int res;
time_t t;
// obtain last modification time
res = ::stat(fileName.c_str(), &statBuf);
if (res < 0) {
t = time(NULL);
} else {
t = statBuf.st_mtime;
}
#ifndef WIN32 // only available on Win32
localtime_r(&t, &_logsTime);
#else
localtime_s(&_logsTime, &t);
#endif
}
void DailyRollingFileAppender::setMaxDaysToKeep(unsigned int maxDaysToKeep) {
_maxDaysToKeep = maxDaysToKeep;
}
unsigned int DailyRollingFileAppender::getMaxDaysToKeep() const {
return _maxDaysToKeep;
}
void DailyRollingFileAppender::rollOver()
{
std::ostringstream filename_s;
::close(_fd);
filename_s << _fileName << "." << _logsTime.tm_year + 1900 << "-"
<< std::setfill('0') << std::setw(2) << _logsTime.tm_mon + 1 << "-"
<< std::setw(2) << _logsTime.tm_mday << std::ends;
const std::string lastFn = filename_s.str();
::rename(_fileName.c_str(), lastFn.c_str());
_fd = ::open(_fileName.c_str(), _flags, _mode);
const time_t oldest = time(NULL) - _maxDaysToKeep * 60 * 60 * 24;
#ifndef WIN32
#define PATHDELIMITER "/"
#else
#define PATHDELIMITER "\\"
#endif
// iterate over files around log file and delete older with same prefix
const std::string::size_type last_delimiter = _fileName.rfind(PATHDELIMITER);
const std::string dirname((last_delimiter == std::string::npos)? "." : _fileName.substr(0, last_delimiter));
const std::string filname((last_delimiter == std::string::npos)? _fileName : _fileName.substr(last_delimiter+1, _fileName.size()-last_delimiter-1));
#ifndef WIN32 // only available on Win32
struct dirent **entries;
int nentries = scandir(dirname.c_str(), &entries, 0, alphasort);
if (nentries < 0)
return;
for (int i = 0; i < nentries; i++) {
struct stat statBuf;
int res = ::stat(entries[i]->d_name, &statBuf);
if ((res == -1) || (!S_ISREG(statBuf.st_mode))) {
free(entries[i]);
continue;
}
if (statBuf.st_mtime < oldest && strstr(entries[i]->d_name, filname.c_str())) {
const std::string fullfilename = dirname + PATHDELIMITER + entries[i]->d_name;
::unlink(fullfilename.c_str());
std::cout << " Deleting " << fullfilename.c_str() << std::endl;
}
free(entries[i]);
}
free(entries);
#else
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
const std::string pattern = _fileName + "*";
hFind = FindFirstFile(LPTSTR(pattern.c_str()), &ffd); //Ahrost
if (hFind != INVALID_HANDLE_VALUE) {
do {
struct stat statBuf;
const std::string fullfilename = dirname + PATHDELIMITER + ffd.cFileName;
int res = ::stat(fullfilename.c_str(), &statBuf);
if (res != -1 && statBuf.st_mtime < oldest && !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
std::cout << "Deleting " << fullfilename << "\n";
::unlink(fullfilename.c_str());
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
// [XXX] some kind of error happened
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
#endif
}
void DailyRollingFileAppender::_append(const log4cpp::LoggingEvent &event)
{
struct tm now;
time_t t = time(NULL);
#ifndef WIN32 // only available on Win32
bool timeok = localtime_r(&t, &now) != NULL;
#else
bool timeok = localtime_s(&now, &t) == 0;
#endif
if (timeok) {
if ((now.tm_mday != _logsTime.tm_mday) ||
(now.tm_mon != _logsTime.tm_mon) ||
(now.tm_year != _logsTime.tm_year)) {
rollOver();
_logsTime = now;
}
}
log4cpp::FileAppender::_append(event);
}
std::auto_ptr<Appender> create_daily_roll_file_appender(const FactoryParams& params)
{
std::string name, filename;
bool append = true;
mode_t mode = 664;
unsigned int max_days_keep = 0;
params.get_for("daily roll file appender").required("name", name)("filename", filename)("max_days_keep", max_days_keep)
.optional("append", append)("mode", mode);
return std::auto_ptr<Appender>(new DailyRollingFileAppender(name, filename, max_days_keep, append, mode));
}
}
Above source belong to DailyRollingFileAppender.cpp file which is one of the log4cpp files.
Your problem is mix-up of narrow and wide strings.
Your use of character types with letter T in the name (like LPTSTR) implies that the code can work with either narrow or wide chars, depending on the _UNICODE setting. However, you use std::string types too, which are explicitly narrow, and tring literals without wide-char specifier L.
If your code SHOULD be available in both narrow and wide favors, make sure you use TEXT() or _T() macros to form the appropriate string.
Hello here is a test code I wrote on MSVC12.
Could someone tell me why the std::move when I pass parameters to the thread are not converting the variabes to RValue refs??
And what I should do.
Thank you!
///some arbitrary long task
std::string DumpFile(std::string path){
std::this_thread::sleep_for(std::chrono::seconds(10));
return path;
}
void run_promise(std::promise<std::string> &&_prom, std::string &&_path){
try {
std::string val = DumpFile(std::move(_path));
_prom.set_value(val);
}
catch (...) {
_prom.set_exception(std::current_exception());
}
}
std::future<std::string> ADumpFile(std::string && path) {
std::promise<std::string> prms;
std::future<std::string> fut = prms.get_future();
std::thread th(run_promise, std::move(prms), std::move(path));
th.detach();
return fut;
}
int main(int argc, char* argv []){
auto fut = ADumpFile("toto");
while (fut.wait_for(std::chrono::seconds(1))!=std::future_status::ready){
std::cout << "waiting\n";
}
auto res = fut.get();
getchar();
return 0;
}
The error I get is :
Error 1 error C2664: 'void (std::promise<std::string> &&,std::string &&)' : cannot convert argument 2 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'std::string &&' C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional 1149 1 demo11
Error 2 error C2664: 'void (std::promise<std::string> &&,std::string &&)' : cannot convert argument 1 from 'std::promise<std::string>' to 'std::promise<std::string> &&' C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\functional 1149 1 demo11
Although, if I call run_promise(std::move(prom),std::move(path)) I have no problem.
Is this an issue related to passing the rvalue arguments through the thread CTOR?
Well, this problem was reported for VC11 http://connect.microsoft.com/VisualStudio/feedback/details/737812 and seems MS didn't address in VC12.
I'm trying to extend Python with C++. I'm using Visual C++ 2008 and Python 2.7. I have had a lot of problems building the .dll file, and finally when it seemed to be everything correct, I can't stop getting this error:
error LNK2001: unresolved external symbol _imp_Py_InitModule4
I know it isn't a linker error because I had this error before (it gave me the error but with all kind of Py_... functions) and I had resolved that.
I don't know if this is an important data but I have build python27_d.dll with VC++ 2008 too.
This is the code:
#include "Python.h"
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <stdlib.h>
#include <Aclapi.h>
struct file_perms {
char user_domain[2050];
unsigned long user_mask;
};
void lookup_sid ( ACCESS_ALLOWED_ACE* pACE, char user_domain[] ) {
char username[1024]="";
char domain[1024]="";
ULONG len_username = sizeof(username);
ULONG len_domain = sizeof(domain);
PSID pSID =(PSID)(&(pACE->SidStart));
SID_NAME_USE sid_name_use;
LPWSTR username1 = reinterpret_cast<LPWSTR>( username );
LPWSTR domain1 = reinterpret_cast<LPWSTR>( domain );
if (!LookupAccountSid(NULL, pSID, username1, &len_username, domain1, &len_domain, &sid_name_use)){
strcpy(user_domain, "unknown");
} else {
strcat(user_domain,domain);
strcat(user_domain,"\\");
strcat(user_domain,username);
}
}
void acl_info( PACL pACL, ULONG AceCount, file_perms fp[]){
for (ULONG acl_index = 0;acl_index < AceCount;acl_index++){
ACCESS_ALLOWED_ACE* pACE;
if (GetAce(pACL, acl_index, (PVOID*)&pACE))
{
char user_domain[2050]="";
lookup_sid(pACE,user_domain);
strcpy(fp[acl_index].user_domain,user_domain);
fp[acl_index].user_mask=(ULONG)pACE->Mask;
}
}
}
static PyObject *get_perms(PyObject *self, PyObject *args)
{
PyObject *py_perms = PyDict_New();
//get file or directory name
char *file;
if (!PyArg_ParseTuple(args, "s", &file))
return NULL;
//setup security code
PSECURITY_DESCRIPTOR pSD;
PACL pDACL;
//GetNamedSecurityInfo() will give you the DACL when you ask for
//DACL_SECURITY_INFORMATION. At this point, you have SIDs in the ACEs contained in the DACL.
LPWSTR file1 = reinterpret_cast<LPWSTR>( file );
ULONG result = GetNamedSecurityInfo(file1,SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL,
&pDACL, NULL, &pSD);
if (result != ERROR_SUCCESS){ return NULL;}
if (result == ERROR_SUCCESS){
ACL_SIZE_INFORMATION aclSize = {0};
if(pDACL != NULL){
if(!GetAclInformation(pDACL, &aclSize, sizeof(aclSize),
AclSizeInformation)){
return NULL;
}
}
file_perms *fp = new file_perms[aclSize.AceCount];
acl_info(pDACL, aclSize.AceCount, fp );
//Dict
for (ULONG i=0;i<sizeof(fp);i++){
PyObject *domain = Py_BuildValue("s",fp[i].user_domain);
PyObject *user = Py_BuildValue("s",fp[i].user_mask);
PyDict_SetItem(py_perms,domain,user);
}
}
return py_perms;
};
static PyMethodDef fileperm_methods[] = {
{ "get_perms", get_perms, METH_VARARGS, "Execute a shell command." },
{ NULL, NULL, 0, NULL }
};
extern "C"
__declspec(dllexport)
void init_fileperm(void)
{
PyObject *m=Py_InitModule("fileperm",fileperm_methods);
return;
}
I'm working in Windows 7 64bits.
I know that Py_InitModule is deprecated for Python 3 but I'm working in Python27 (2.7.3 ).
Does someone know why I get this error?
Thanks!
I had the same problem.
If you're compiling a 64-bit pyd, make sure python27.lib is also 64-bit (same goes for compiling a 32-bit pyd with a 32-bit python27.lib).