首页 \ 问答 \ 如何检查一个文件是否已被C中的另一个进程打开?(How to check if a file is already open by another process in C?)

如何检查一个文件是否已被C中的另一个进程打开?(How to check if a file is already open by another process in C?)

我看到标准C无法告诉文件是否已经在另一个进程中打开。 所以答案应该包含每个平台的几个例子。 尽管我需要检查Visual C ++ / Windows。


I see that standard C has no way of telling if a file is already opened in another process. So the answer should contain several examples for each platform. I need that check for Visual C++ / Windows though.


原文:https://stackoverflow.com/questions/1951791
更新时间:2024-05-25 16:05

最满意答案

我刚刚为此找到了一个解决方案: https//apple.stackexchange.com/a/123738/72534

#import <objc/runtime.h>
#import <Foundation/Foundation.h>

int main () {
    NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/Utilities/Keychain Access.app/Contents/Resources/Keychain.menu"];

    Class principalClass = [bundle principalClass];

    id instance = [[principalClass alloc] init];

    [instance performSelector:@selector(_lockScreenMenuHit:) withObject:nil];

    return 0;
}

另存为lockscreen.m并编译为:

clang -framework Foundation -o lockscreen lockscreen.m

完美工作,因为它只是调用与状态栏图标相同的例程。


I just found a solution for this: https://apple.stackexchange.com/a/123738/72534

#import <objc/runtime.h>
#import <Foundation/Foundation.h>

int main () {
    NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/Utilities/Keychain Access.app/Contents/Resources/Keychain.menu"];

    Class principalClass = [bundle principalClass];

    id instance = [[principalClass alloc] init];

    [instance performSelector:@selector(_lockScreenMenuHit:) withObject:nil];

    return 0;
}

Save as lockscreen.m and compile with:

clang -framework Foundation -o lockscreen lockscreen.m

Works perfectly, as it just calls the same routine as the status bar icon.

相关问答

更多

相关文章

更多

最新问答

更多
  • 谁有JAVA的视频教程啊 最好从基础开始到精通 打包好的
  • 仅根据表格的一列删除重复的值(Remove duplicate values based on only one column of the table)
  • 从数据库值填充数组的最有效方法?(Most efficient way to populate array from database values?)
  • 我在data属性中有一个变量,并从prop传递相同的变量。(I have one variable in data attribute, and pass the same variable from prop. why the {{}} template display the value from prop rather than data)
  • Reg Ex Django Url Conf(Reg Ex Django Url Conf)
  • 本地化不适用于主要活动(Localization does not work on main activity)
  • 我有一个同学让我去福州科闽计算机学校学习室内设计,这个学校好吗?
  • 变量的模板参数中的Decltype(Decltype in template parameter of variable)
  • 代码签名错误与Xcode 4.2(Code Sign error with Xcode 4.2)
  • 在保留表达式生命周期的同时强制执行参数评估顺序(Enforce parameter evaluation order while retaining lifetime of of expressions)
  • 查询以获取早于开始日期的日期行(不是简单的WHERE)(Query to get date rows older than a start date (not a simple WHERE))
  • 武清哪家会计培训机构通过率高?
  • Java正则表达式逻辑OR(Java regex logical OR)
  • 使用for循环创建异步Vue组件(Use for loop to create async Vue component)
  • 从magento数据库获取客户名称和电子邮件(Get customer names and emails from magento database)
  • 运行Ruby冒泡排序(Running Through a Ruby Bubble Sort)
  • 减去返回多于1行的两个SQL子查询的结果(Subtracting the results of the two SQL subqueries that returns more than 1 row)
  • 对于Ruby http请求,响应“呃?”是间歇性的(The response “er ?” is getting intermittently for Ruby http request)
  • 请问嘉定区有没有培训上海市电脑中级的,有的话在哪里?
  • 根据日期范围查询最小分区键(聚类键)(Query min partition key based on date range (clustering key))
  • int listinsert(sqlist * & L)中的&是什么意思
  • 原始套接字的会话管理(Session Management on Raw Socket)
  • 将Type用作“属性”是不好的做法?(Is it bad practise to use Type as a 'Property'?)
  • 电脑等级证书二级和三级有什么区别啊 !!我们学校有二级和三级培训到底该选着那个呢????????
  • linux 安装新硬盘如何设置权限
  • 无法从RecyclerView的onBindViewHolder更改根视图的大小或位置(Cannot change root view's size or position from RecyclerView's onBindViewHolder)
  • spin.js没出现在我的网站上?(spin.js is not showing up on my site?)
  • Gulp + Bootstrap字体(Gulp + Bootstrap Fonts)
  • 如何使用短信Alfresco.util.PopupManager.displayPrompt显示图标(How to show icon with text message Alfresco.util.PopupManager.displayPrompt)
  • Skype.getAllChats()未检测到基于云的群组聊天(Skype.getAllChats() not detecting cloud-based group chats)