posts - 319, comments - 22, trackbacks - 0, articles - 11
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

转载:http://www.cnblogs.com/fzzl/archive/2009/07/14/1522913.html

【转】vs2005下的dirent.h  该方法同样适用于VS2008 及VS2010

http://www.analogcn.com/Article/wz3/200802/20080202211037.html

 

dirent.h是gcc下的一个头文件,而在VS2005中是没有的。这个文件中封装了几个对目录进行操作函数:

static DIR *opendir (const char *dirname);
static struct dirent *readdir (DIR *dirp);
static int closedir (DIR *dirp);

 对于在linux->windows之间进行程序移植来讲常常会造成一些困扰,在网上仔细搜了一下,发现原来已经有位好同志写了相应的移植代码,如下所示:


typedef struct dirent {
  /* name of current directory entry (a multi-byte character string) */
  char d_name[MAX_PATH + 1];

  /* file attributes */
  WIN32_FIND_DATA data;
} dirent;


typedef struct DIR {
  /* current directory entry */
  dirent current;

  /* is there an un-processed entry in current? */
  int cached;

  /* file search handle */
  HANDLE search_handle;

  /* search pattern (3 = zero terminator + pattern "\\*") */
  TCHAR patt[MAX_PATH + 3];
} DIR;


static DIR *opendir (const char *dirname);
static struct dirent *readdir (DIR *dirp);
static int closedir (DIR *dirp);


/* use the new safe string functions introduced in Visual Studio 2005 */
#if defined(_MSC_VER) && _MSC_VER >= 1400
# define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
#else
# define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
#endif


/*
 * Open directory stream DIRNAME for read and return a pointer to the
 * internal working area that is used to retrieve individual directory
 * entries.
 */
static DIR*
opendir(
    const char *dirname)
{
  DIR *dirp;
  assert (dirname != NULL);
  assert (strlen (dirname) < MAX_PATH);

  /* construct new DIR structure */
  dirp = (DIR*) malloc (sizeof (struct DIR));
  if (dirp != NULL) {
    TCHAR *p;
   
    /* prepare search pattern */
#ifdef _UNICODE

    /* convert directory name to wide character string */
    MultiByteToWideChar(
        CP_ACP,                                /* code page */
        0,                                     /* conversion flags */
        dirname,                               /* mb-string to convert */
        -1,                                    /* length of mb-string */
        dirp->patt,                            /* wc-string to produce */
        MAX_PATH);                             /* max length of wc-string */
    dirp->patt[MAX_PATH] = '\0';
   
    /* append search pattern to directory name */
    p = wcschr (dirp->patt, '\0');
    if (dirp->patt < p  &&  *(p-1) != '\\'  &&  *(p-1) != ':') {
      *p++ = '\\';
    }
    *p++ = '*';
    *p = '\0';

#else /* !_UNICODE */
   
    /* take directory name... */
    STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
    dirp->patt[MAX_PATH] = '\0';
   
    /* ... and append search pattern to it */
    p = strchr (dirp->patt, '\0');
    if (dirp->patt < p  &&  *(p-1) != '\\'  &&  *(p-1) != ':') {
      *p++ = '\\';
    }
    *p++ = '*';
    *p = '\0';
   
#endif /* !_UNICODE */

    /* open stream and retrieve first file */
    dirp->search_handle = FindFirstFile (dirp->patt, &dirp->current.data);
    if (dirp->search_handle == INVALID_HANDLE_VALUE) {
      /* invalid search pattern? */
      free (dirp);
      return NULL;
    }

    /* there is an un-processed directory entry in memory now */
    dirp->cached = 1;
   
  }
  return dirp;
}


/*
 * Read a directory entry, and return a pointer to a dirent structure
 * containing the name of the entry in d_name field.  Individual directory
 * entries returned by this very function include regular files,
 * sub-directories, pseudo-directories "." and "..", but also volume labels,
 * hidden files and system files may be returned. 
 */
static struct dirent *
readdir(
    DIR *dirp)
{
  assert (dirp != NULL);

  if (dirp->search_handle == INVALID_HANDLE_VALUE) {
    /* directory stream was opened/rewound incorrectly or it ended normally */
    return NULL;
  }

  /* get next directory entry */
  if (dirp->cached != 0) {
    /* a valid directory entry already in memory */
    dirp->cached = 0;
  } else {
    /* read next directory entry from disk */
    if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {
      /* the very last file has been processed or an error occured */
      FindClose (dirp->search_handle);
      dirp->search_handle = INVALID_HANDLE_VALUE;
      return NULL;
    }
  }

  /* copy directory entry to d_name */
#ifdef _UNICODE
 
  /* convert entry name to multibyte */
  WideCharToMultiByte(
      CP_ACP,                                  /* code page */
      0,                                       /* conversion flags */
      dirp->current.data.cFileName,            /* wc-string to convert */
      -1,                                      /* length of wc-string */
      dirp->current.d_name,                    /* mb-string to produce */
      MAX_PATH,                                /* max length of mb-string */
      NULL,                                    /* use sys default character */
      NULL);                                   /* don't care  */
  dirp->current.d_name[MAX_PATH] = '\0';
 
#else /* !_UNICODE */

  /* copy as a multibyte character string */
  STRNCPY (dirp->current.d_name, dirp->current.data.cFileName, sizeof(dirp->current.d_name));
  dirp->current.d_name[MAX_PATH] = '\0';

#endif /* !_UNICODE */
 
  return &dirp->current;
}


/*
 * Close directory stream opened by opendir() function.  Close of the
 * directory stream invalidates the DIR structure as well as any previously
 * read directory entry.
 */
static int
closedir(
    DIR *dirp)
{
  assert (dirp != NULL);
 
  /* release search handle */
  if (dirp->search_handle != INVALID_HANDLE_VALUE) {
    FindClose (dirp->search_handle);
    dirp->search_handle = INVALID_HANDLE_VALUE;
  }

  /* release directory handle */
  free (dirp);
  return 0;
}

此文件可从http://www.softagalleria.net/dirent/index.en.html下载得到,直接将它放在VS2005的include目录就OK 了!

开网店http://www.5678520.com/怎么样开网店

posted @ 2012-04-28 06:42 RTY 阅读(9557) | 评论 (0)编辑 收藏

     摘要: 2011-04-15 11:09 46人阅读 评论(0) 收藏 举报摘自msdn,列在这里方便查阅。The following tables show the format specifiers recognized by the debugger. SpecifierFormatExpressionValue Displayedd,isigned...  阅读全文

posted @ 2012-04-24 22:37 RTY 阅读(683) | 评论 (1)编辑 收藏

     摘要: HomeLibraryLearnDownloadsSupportCommunitySign in | 中国(简体中文) |  | MSDN LibraryDevelopment Tools and LanguagesVisual Studio 2008Visual StudioApplication Development in Visu...  阅读全文

posted @ 2012-04-24 21:42 RTY 阅读(734) | 评论 (0)编辑 收藏

     摘要: HomeLibraryLearnDownloadsSupportCommunitySign in | 中国(简体中文) |  | MSDN LibraryDevelopment Tools and LanguagesVisual Studio 2010Visual StudioApplication Development in Visu...  阅读全文

posted @ 2012-04-24 21:39 RTY 阅读(586) | 评论 (0)编辑 收藏

Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Find path of an application


  • SubjectRe: Find path of an application
  • From: Terry Lambert <email@hidden>
  • Date: Tue, 16 Dec 2008 02:01:54 -0800
  • Delivered-to: email@hidden
  • Delivered-to: email@hidden

On Dec 16, 2008, at 1:00 AM, Rakesh Singhal wrote:
I am done with finding the process is running or not. Thanks a lot. Still I am stuck with second issue to find the path to my application in my system. Actually there are 3 steps:

1. To know that application is running or not?  Now it is done. 2. If not then get the path of application where it was installed (user can change the path during installation). 3. Launch the application.

My code is standard C++ tool so I want to use only C and C++ APIs. Please suggest me. 

You said this was a GUI app that you didn't control the sources to. So control it anyway by renaming the binary in the bundle and putting a stub in there that will save off the id for you and then reexec the real binary:


--- example with no error checking --- #include <mach-o/dyld.h>	/* _NSGetExecutablePath */ #include <limits.h>		/* PATH_MAX */ #include <unistd.h>		/* execve */ #include <libgen.h>		/* dirname */ #include <string.h>		/* strcpy */

#define BINARYNAME	"myreal_executable"

int main(int ac, char *av[]) { 	char pathbuf[PATH_MAX + 1]; 	char real_executable[PATH_MAX + 1]; 	char *bundle_id; 	int  bufsize = sizeof(pathbuf);

	_NSGetExecutablePath( pathbuf, &bufsize);

	bundle_id = dirname(pathbuf);

	strcpy(real_executable, bundle_id); 	strcat(real_executable, "/"); 	strcat(real_executable, BINARYNAME);

	execv(real_executable, av); } --------------------

Then write the path into /var/run/program.<pid> before you do the execv call to give control to the real binary.

Then in your other program go looking for /var/run/program.*. When you find one, take the pid and do an atoi() on it to get the integer pid back. Then end it a kill(pid, 0).

This function will return one of three things:

(1) 0, indicating that the process exists and you have the right to send it signals

(2) -1, with errno set to EPERM, indicating that the process exists and you do not have rights to send it a signal

(3) -1, with errno set to ESRCH, indicating that the process does not (yet) exist

-

Ideally, all this would be unnecessary because you put your daemon and the program you want to give it a UI into the same bundle, which ,means either one of them can find the other by looking at the dirname() from their own call to _NSGetExecutablePath().

No grovelling around trying to find out where something came from, because it tells you.

-- Terry _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list      (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden  
References: 
 >Find path of an application (From: "Rakesh Singhal" <email@hidden>)
 >Re: Find path of an application (From: Jean-Daniel Dupas <email@hidden>)
 >Re: Find path of an application (From: "Rakesh Singhal" <email@hidden>)
 >Re: Find path of an application (From: Jean-Daniel Dupas <email@hidden>)
 >Re: Find path of an application (From: "Rakesh Singhal" <email@hidden>)

posted @ 2012-04-18 22:44 RTY 阅读(570) | 评论 (0)编辑 收藏

@executable path, @load path and @rpath

2010年11月6日 by Wincent Colaiuta

Note: this article is actually about the @executable_path, @load_path and @rpath install paths used by the linker on Mac OS X; wiki titles can't include underscores, however, because they are ambiguous with spaces.

Absolute paths

Useful for frameworks installed in shared locations. Example:

  • Install path: /Library/Frameworks/Foo.framework/Versions/A/Foo

@executable_path

Useful for frameworks embedded inside applications, because it allows you to specify the location of the framework relative to the application's executable:

  • Install path: @executable_path/../Frameworks/Foo.framework/Versions/A/Foo
  • Application location: /Applications/Foo.app
  • Executable path: /Applications/Foo.app/Contents/MacOS
  • Framework location: /Applications/Foo.app/Contents/Frameworks/Foo.framework
  • Linker puts all this together to figure out that the framework binary can be found at: /Applications/Foo.app/Contents/MacOS/../Frameworks/Foo.framework/Versions/A/Foo

@loader_path

Available from Mac OS X 10.4 Tiger onwards; useful for frameworks embedded inside plug-ins, because it allows you to specify the location of the framework relative to the plug-in's code (remember, plug-ins may not actually know where they are going to be installed, relative to the application, so knowing @executable_path doesn't help us in this case):

  • Install path: @loader_path/../Frameworks/Foo.framework/Versions/A/Foo
  • Application location: /Applications/Foo.app
  • Plug-in location: /Library/Application Support/Foo/Plug-Ins/Bar.bundle
  • Executable path: /Applications/Foo.app/Contents/MacOS
  • Loader path: /Library/Application Support/Foo/Plug-Ins/Bar.bundle/Contents/MacOS
  • Framework location: /Library/Application Support/Foo/Plug-Ins/Bar.bundle/Contents/Frameworks/Foo.framework
  • Linker puts all this together to figure out that the framework binary can be found at: /Library/Application Support/Foo/Plug-Ins/Bar.bundle/Contents/MacOS/../Frameworks/Foo.framework/Versions/A/Foo

Note that if the "loader" is an application rather than a plug-in, the @loader_path ends up being equivalent to @executable_path.

@rpath

New in Mac OS X 10.5 Leopard is @rpath. Key points:

  • @rpath instructs the dynamic linker to search a list of paths in order to locate the framework
  • critically, this list is embedded in the loading application
  • this means that a single framework with @rpath/Foo.framework/Versions/A/Foo can be made to work in a number of different ways; that is, you are effectively no longer limited by the choice of specifying your "install path" using either @executable_path or @loader_path
  • the down side: you now have to pass additional linker flags when building the host application (eg. -rpath @executable_path/../Frameworks or /Library/Frameworks; note that specifying both will cause the dynamic linker to try looking in both locations)

Sources

posted @ 2012-04-18 06:14 RTY 阅读(1410) | 评论 (0)编辑 收藏

真的坑爹,今天才开始玩MAC OX,装了个最新版本的10.7.3,只能装XCODE 4.3 这个月刚发行的版本。

安装时发现没有install过程,直接双击就进入开发环境了。而且装完后没有gcc 等各种编译工具,在TERMINAL下各种命令不识别,想装ruby的各种开发工具,都不行了。

 

查了半天才发现:

Apple announced Xcode 4.3 for OSX Lion and 4.4 for OSX Mountain Lion last week. The major difference is that Xcode no longer provide an installer which is good thing because you now could update Xcode with AppStore in the future, plus it is much easier to carry the development environment with you. However, there is a little problem with this new version of Xcode, is that all command line toolsets and compilers are not visible in terminal. 

解决方案一:
A simple fix is to update your PATH env:

export PATH=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:$PATH

Please be noted that clang does not reside in /Developer/usr/bin, it is now in /Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Now you could access to gcc, g++, git or any toolsets bundled with Xcode. For your convenience, it is recommended to include it in your .bash_profile.

解决方案二:

You can install these additional tools directly in Xcode :
Preferences > Downloads > Command Line Tools > Install


SO. What a fUUUcking day!

posted @ 2012-04-07 21:50 RTY 阅读(708) | 评论 (0)编辑 收藏

原文:http://blog.sina.com.cn/s/blog_5a6efa330100x3sp.html

10月12日全新的IOS 5系统可供下载后,Mac OS也升级到了10.7.2,10.7.2支持iCloud 全套云服务,用户可以将自己的数据自动存储到iCloud中并推送到所有设备。另外本次更新主要包括常规性修复,增强了稳定性、兼容性和安全性等。
但很多黑苹果的朋友在更新10.7.2版本后,发现系统无法启动,出现五国或者禁止符号等错误。本文只针对采用虚拟机(VMware或者VirtualBOX)的朋友,具体解决方案如下;
原因就是:升级后AppleLSIFusionMPT.kext 出了问题,这个可以在升级补丁前备份AppleLSIFusionMPT.kext,完了后AppleLSIFusionMPT.kext备份到之前的文件夹。
具体解决方法是:
1、首先正常启动虚拟机后,进入Mac OS X 10.7.1操作系统内。

原版本为:11B26

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案

2、启动“终端”程序(在“前往-实用工具”中)
3、在终端命令行下完整输入如下引号内的命令(意思是备份AppleLSIFusionMPT.kext文件到当前目录)
“cp -rv /System/Library/Extensions/AppleLSIFusionMPT.kext  .“
注意最后一个点号不要忘记(指备份到当前目录),而且大小写也不能错(我没试过全部小写,我印象中Unix系统都是大小写敏感的,这一点和windows不一样)
输入之后,按回车,会出现一大堆文字,表示一些文件被正常备份下来。
VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案

 
4、不要关闭终端,然后开始正常的10.7.2补丁升级操作(这个步骤不会很快,尤其是在虚拟机下,可以干点别的,或者看我继续往下说)
苹果官方10.7.2升级包地址
http://support.apple.com/downloads/DL1459/en_US/MacOSXUpdCombo10.7.2.dmg
5、【切记】顺利升级完成后,不要立刻启动操作系统!而是重新回到终端命令行下
6、输入如下引号内命令(意思是删除在Lion10.7.2升级过程中系统又安装上的AppleLSIFusionMPT.kext,这个文件是Lion10.7.2版本的,不好用,必须先删除!)
”sudo rm -rfv /System/Library/Extensions/AppleLSIFusionMPT.kext“
回车,会提示你输入密码,输入你登录密码即可。可能屏幕没有显示,所以你一定要看好了提示文字(懂点英文还是必须的)
 VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


再输入引号内命令(意思是将升级前备份在当前目录下的AppleLSIFusionMPT.kext文件,是Lion 10.7.1版本的,重新拷贝到原系统中)
”sudo cp -rv AppleLSIFusionMPT.kext /System/Library/Extensions“

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


7、正常重启,大功告成!看到版本已经变成10.7.2了。版本号为:Mac OS X Lion (11C74)
 VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案
    Lion 10.7.2的主要功能之一,iCloud出来了。

这个修改版起码小的升级都没有问题,而且常用的功能都使用正常,起码我的iphone连上了itunes
假如已经升级了,显然是无法启动的,那么可以用笔者以下的解决方案来解决。实现原理就是使用Windows PE盘进入系统,然后利用Transmac工具(至于为什么不用Macdrive,因为它不能在PE下使用,而且还不免费)打开系统盘/System/Library/Extensions/,将AppleLSIFusionMPT.kext替换,可能需要两次才能生效,但是此教程是一定能够成功的。图文教程如下;(当然也是从insanelymac学来的),我直接把重要的地方粘贴过来了(部分加上了细节)
*本文将利用到的工具*(Transmac和AppleLSIFusionMPT.kext文件)
下载地址: http://m1.mail.sina.com.cn/apps/netdisk/download.php?id=e6f5482c11f317df981ddb0f1307cfca
Mac_OS_X_10.7.2系统替换文件和工具by_dehe1988.rar
 VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案
使用PE工具进入系统,本人使用的是“深山红叶”大神工具包。PS:各种PE系统都行,这个只是提供一个修改的环境!
 

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案
系统界面
 
VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案



输入下载地址。如果很懒,也可以先下载,再发送到自己的邮箱中,再登陆自己邮箱下载,貌似更复杂?
 VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


文件约1.5MB,很好下载的!
 VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


Transmac的界面,该软件可以读取览苹果文件系统HFS+,而且可以在PE环境下运行。但Transmac默认只能读取,因此需要设置。 
 VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


容许读写打开。关闭,再一次打开就会生效了。
 
VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


找到/System/Library/Extensions/目录下,将AppleLSIFusionMPT.kext复制到此替换。

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


 VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案

VMware虚拟机从Mac <wbr>OS <wbr>Lion <wbr>10.7.1更新到Mac <wbr>OS <wbr>Lion <wbr>10.7.2后无法正常启动解决方案


 

posted @ 2012-04-07 14:24 RTY 阅读(1412) | 评论 (0)编辑 收藏

     摘要: 原文:http://ideapad.zol.com.cn/56/160_557572.html灵感来了的时候,挡也挡不住,这个是真的!昨晚一个很偶然的机会下,我使用Vmware 8 安装成功了Mac OS X 10.7(Lion)系统,实在是让人非常兴奋。再三申明,本人的电脑就是那种装黑苹果被判了死刑的电脑。关于死——乔布斯是这么说的“没有人愿意死,即使想上天堂...  阅读全文

posted @ 2012-04-07 07:45 RTY 阅读(3228) | 评论 (0)编辑 收藏

     摘要: 原文:http://ideapad.zol.com.cn/57/160_560764.html楼主我之前发过的Vmware 8和VirtualBOX 安装Lion的教程。1、全民吃苹果,首发VirtualBOX安装Mac OS X 10.7正式版_可完美升级,无bug http://ideapad.zol.com.cn/56/160_557572.html2、踏破铁鞋,Vmware 8完...  阅读全文

posted @ 2012-04-07 07:32 RTY 阅读(835) | 评论 (0)编辑 收藏

仅列出标题
共31页: 1 2 3 4 5 6 7 8 9 Last