我可以用 Objective-C 代码获取系统代理,但我想用 c++ 编写它。你能帮我将 Objective-C 代码转换为 C++ 吗?

发布于 2024-12-25 17:41:27 字数 700 浏览 0 评论 0原文

我对 Objective-C 不熟悉,所以我很难理解它并将其转换为 C++。 Objective-C代码如下,它将获取iOS系统代理。你能帮我把它转换成c++吗?

NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]);  
NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com"], (CFDictionaryRef)proxySettings) autorelease]);  
NSDictionary *settings = [proxies objectAtIndex:0];  
NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);  
NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);  
NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);  

I'm not familiar with objective-c, so it is hard for me to understand it and convert it to c++.
The objective-c code as follows, it will get the iOS system proxy. Could you help me to convert it to c++?

NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]);  
NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com"], (CFDictionaryRef)proxySettings) autorelease]);  
NSDictionary *settings = [proxies objectAtIndex:0];  
NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);  
NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);  
NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2025-01-01 17:41:27

您找到的代码示例已经使用了 C API(CFNetworkCopySystemProxySettings、CFNetworkCopyProxiesForURL),然后将进行一些额外的工作,将结果转换为对 Objective-C 程序更友好的内容(例如,调用 NSMakeCollectable 来启用 Objective-C 垃圾处理)收藏)。

如果您知道“CF*”调用是“Core Foundation”API,可以从 C(或 C++)调用,而“NS*”是 Objective-C 类/方法,那么它可能会帮助您理解这一点。因此,您的任务是仅使用 Core Foundation 调用 & 来重新转换代码。数据类型。例如,CFDictionary 和 NSDictionary 是可以互换的(用 Apple 的说法是“免费桥接”),因此您可以使用 CFDictionary API 而不是 NSDictionary 来处理 C/C++ 程序中的字典。

要了解它是如何工作的(而不是仅仅要求其他人为您翻译),您需要研究如下功能:

CFNetworkCopySystemProxySettings

CFURLCreateWithString

CFNetworkCopyProxiesForURL

CFDictionaryGetValue

您还需要对 Core Foundation内存管理规则

The code example you found is already using C APIs (CFNetworkCopySystemProxySettings, CFNetworkCopyProxiesForURL), and is then going to some additional work to convert the results to something more friendly for an Objective-C program (for example, calling NSMakeCollectable to enable Objective-C garbage collection).

It may help you to understand this if you know that the "CF*" calls are "Core Foundation" API, which is callable from C (or C++), and "NS*" things are Objective-C classes/methods. So your task is to re-cast the code using just the Core Foundation calls & datatypes. For example, CFDictionary and NSDictionary are interchangeable ("toll-free bridged", in Apple parlance), so you can handle the dictionary from a C/C++ program by using the CFDictionary API instead of NSDictionary.

To understand how this is working (instead of just asking someone else to translate it for you), you will want to research functions like:

CFNetworkCopySystemProxySettings

CFURLCreateWithString

CFNetworkCopyProxiesForURL

CFDictionaryGetValue

You'll also need a basic understanding of Core Foundation memory management rules

断爱 2025-01-01 17:41:27

谢谢大卫·盖尔哈。根据您的建议,我终于写出了代码,它可以在iOS和MAC上运行:

int CHTTP::GetMacProxy() {    
  CFURLRef        urlRef = NULL;
  CFDictionaryRef proxyDicRef = NULL;
  CFArrayRef      urlProxArrayRef = NULL;
  CFDictionaryRef defProxyDic = NULL;
  int             port = 0;
  CFStringRef     hostNameRef = NULL;
  CFNumberRef     portNumberRef = NULL;
  char            hostNameBuffer[200];

  urlRef = CFURLCreateWithBytes(kCFAllocatorDefault, (const UInt8*)m_URL.Deref(), m_URL.Length(), kCFStringEncodingASCII, NULL);
  if (!urlRef) goto done;

  proxyDicRef = CFNetworkCopySystemProxySettings();
  if (!proxyDicRef) goto done;

 urlProxArrayRef = CFNetworkCopyProxiesForURL(urlRef, proxyDicRef);
  if (!urlProxArrayRef) goto done;

  defProxyDic = (CFDictionaryRef)CFArrayGetValueAtIndex(urlProxArrayRef, 0);
  if (!defProxyDic) goto done;

  portNumberRef = (CFNumberRef)CFDictionaryGetValue(defProxyDic, (const void*)kCFProxyPortNumberKey);
  if (!portNumberRef) goto done;
  if (!CFNumberGetValue(portNumberRef, kCFNumberSInt32Type, &port)) goto done;

  hostNameRef = (CFStringRef)CFDictionaryGetValue(defProxyDic, (const void*)kCFProxyHostNameKey);
  if (!hostNameRef) goto done;


  if (!CFStringGetCString(hostNameRef, hostNameBuffer, 200, kCFStringEncodingASCII)) goto done;

  // Log port
  // Log hostNameBuffer.


done:
  if (hostNameRef)     {CFRelease(hostNameRef); hostNameRef = NULL;}
  if (urlProxArrayRef) {CFRelease(urlProxArrayRef); urlProxArrayRef = NULL;}
  if (proxyDicRef)     {CFRelease(proxyDicRef); proxyDicRef = NULL;}
  if (urlRef)          {CFRelease(urlRef); urlRef = NULL;}

  return 0;

}

Thanks David Gelhar. With your suggestion, I wrote out the code at last, it can work on iOS and MAC:

int CHTTP::GetMacProxy() {    
  CFURLRef        urlRef = NULL;
  CFDictionaryRef proxyDicRef = NULL;
  CFArrayRef      urlProxArrayRef = NULL;
  CFDictionaryRef defProxyDic = NULL;
  int             port = 0;
  CFStringRef     hostNameRef = NULL;
  CFNumberRef     portNumberRef = NULL;
  char            hostNameBuffer[200];

  urlRef = CFURLCreateWithBytes(kCFAllocatorDefault, (const UInt8*)m_URL.Deref(), m_URL.Length(), kCFStringEncodingASCII, NULL);
  if (!urlRef) goto done;

  proxyDicRef = CFNetworkCopySystemProxySettings();
  if (!proxyDicRef) goto done;

 urlProxArrayRef = CFNetworkCopyProxiesForURL(urlRef, proxyDicRef);
  if (!urlProxArrayRef) goto done;

  defProxyDic = (CFDictionaryRef)CFArrayGetValueAtIndex(urlProxArrayRef, 0);
  if (!defProxyDic) goto done;

  portNumberRef = (CFNumberRef)CFDictionaryGetValue(defProxyDic, (const void*)kCFProxyPortNumberKey);
  if (!portNumberRef) goto done;
  if (!CFNumberGetValue(portNumberRef, kCFNumberSInt32Type, &port)) goto done;

  hostNameRef = (CFStringRef)CFDictionaryGetValue(defProxyDic, (const void*)kCFProxyHostNameKey);
  if (!hostNameRef) goto done;


  if (!CFStringGetCString(hostNameRef, hostNameBuffer, 200, kCFStringEncodingASCII)) goto done;

  // Log port
  // Log hostNameBuffer.


done:
  if (hostNameRef)     {CFRelease(hostNameRef); hostNameRef = NULL;}
  if (urlProxArrayRef) {CFRelease(urlProxArrayRef); urlProxArrayRef = NULL;}
  if (proxyDicRef)     {CFRelease(proxyDicRef); proxyDicRef = NULL;}
  if (urlRef)          {CFRelease(urlRef); urlRef = NULL;}

  return 0;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文