稳定盈利的期货交易方法-量化趋势交易

alantop -专业量化投资者

爱好:量化投资,逆向工程,渗透
随笔 - 595, 文章 - 0, 评论 - 921, 引用 - 0
数据加载中……

vc++如何调用.Ner Framewrok类库 ( 读写注册表示例)(CLI的一些应用)

.net Framework类库越来越丰富了。伴随vs2008已经3.5版本了。

以前写程序很少会去调用.net framework,不过随着微软的主推,和不断对其进行扩展。

其功能正在逐渐强大,使用更加简洁和方便。

这里通过对注册表的操作。我们来看看vc++.net程序。

说明:以下代码在vs2005下编译。

简单的说传统的c++如何调用net frame.

一些预备知识:

^:vc2005下,声明托管堆(the managed heap)上对象的句柄。

在vc++2002和vc++2003下,声明托管堆上对象的句柄是用“__gc *”表示。

为什么会出现"^"符号呢?

在netframe下,为了保持托管堆上的对象的引用能够被垃圾收集器跟踪,且当这个对象被移动的时候,这个句柄可以被及时的更新。而传统的指针和引用无法被正确跟踪,所以产生了^来表示一个托管堆上对象的句柄。

其可以用->来访问其成员。

 

什么时候用 ^ 呢?

来看看RegisterKey的声明

[ComVisibleAttribute(true)] 
public ref class RegistryKey sealed : public MarshalByRefObject, IDisposable
 
看到ref了吧,
ref:声明一个托管类或者结构。
看到它,就声明^
 
以下三个例子应该很容易看懂了。
 

1. 将数据写入注册表

using namespace System;
using namespace Microsoft::Win32;

int main()
{
    // The second OpenSubKey argument indicates that
    // the subkey should be writable.
    RegistryKey^ rk;

//托管堆上的一个句柄
    rk  = Registry::CurrentUser->OpenSubKey("Software", true);

//静态成员可以通过其类名直接访问
    if (!rk)
    {
        Console::WriteLine("Failed to open CurrentUser/Software key");
        return -1;
    }

    RegistryKey^ nk = rk->CreateSubKey("NewRegKey");
    if (!nk)
    {
        Console::WriteLine("Failed to create 'NewRegKey'");
        return -1;
    }

    String^ newValue = "NewValue";
    try
    {
        nk->SetValue("NewKey", newValue);
        nk->SetValue("NewKey2", 44);
    }
    catch (Exception^)
    {
        Console::WriteLine("Failed to set new values in 'NewRegKey'");
        return -1;
    }

    Console::WriteLine("New key created.");
    Console::Write("Use REGEDIT.EXE to verify ");
    Console::WriteLine("'CURRENTUSER/Software/NewRegKey'\n");*/
    return 0;
}

2. 从注册表中读取数据

using namespace System;
using namespace Microsoft::Win32;

int main( )
{
   array<String^>^ key = Registry::CurrentUser->GetSubKeyNames( );

   Console::WriteLine("Subkeys within CurrentUser root key:");
   for (int i=0; i<key->Length; i++)
   {
      Console::WriteLine("   {0}", key[i]);
   }

   Console::WriteLine("Opening subkey 'Identities'...");
   RegistryKey^ rk = nullptr;
   rk = Registry::CurrentUser->OpenSubKey("Identities");
   if (rk==nullptr)
   {
      Console::WriteLine("Registry key not found - aborting");
      return -1;
   }

   Console::WriteLine("Key/value pairs within 'Identities' key:");
   array<String^>^ name = rk->GetValueNames( );
   for (int i=0; i<name->Length; i++)
   {
      String^ value = rk->GetValue(name[i])->ToString();
      Console::WriteLine("   {0} = {1}", name[i], value);
   }

   return 0;
}
 
3. 从注册表中删除subkey.
 
    RegistryKey^ rk;
    rk  = Registry::CurrentUser->OpenSubKey("Software", true);
    if (!rk)
    {
        Console::WriteLine("Failed to open CurrentUser/Software key");
        return -1;
    }
    rk->DeleteSubKey("NewRegKey");

 

 

写在后面的话:

之所有写这个标题,而不写有关CLI的,国内关于cli探讨还是比较少的。

既然,有人有好的建议,加一个副标题好了。

posted on 2007-08-02 16:33 AlanTop 阅读(2101) 评论(6)  编辑 收藏 引用 所属分类: VC++

评论

# re: vc++如何调用.Ner Framewrok类库 ( 读写注册表示例)  回复  更多评论   

在vs2002和2003中,只有Manged C++

在VS2005上新增CLI/C++



其实Manged C++或是CLI/C++ , 都是对MS对C++的扩充,用来在C++中访问。netframework,并且CLI/C++已经在什么欧洲,,,标准。


我的博客上有一些相关的介绍。

2007-08-02 17:33 | 梦在天涯

# re: vc++如何调用.Ner Framewrok类库 ( 读写注册表示例)  回复  更多评论   

别把Native C++和CLI混为一谈哇。。。
2007-08-02 18:51 | 空明流转

# re: vc++如何调用.Ner Framewrok类库 ( 读写注册表示例)  回复  更多评论   

interop技术,作为C++ native代码(非dll、com)向.net技术过渡的一道桥梁。
2007-08-03 10:08 | 万连文

# re: vc++如何调用.Ner Framewrok类库 ( 读写注册表示例)(CLI的一些应用)[未登录]  回复  更多评论   

See See
2007-08-03 15:55 | 111

# re: vc++如何调用.Ner Framewrok类库 ( 读写注册表示例)(CLI的一些应用)  回复  更多评论   

我写了个c++调用.net库的测试程序,结果编译失败,提示是不存在该命名空间,楼主你是怎么编译过去的,是不是需要加什么option啊。测试程序如下:
using namespace System;
int main()
{
Console::WriteLine("Hello World!");
}
2008-08-14 12:16 | 郢都孤鬼

# re: vc++如何调用.Ner Framewrok类库 ( 读写注册表示例)(CLI的一些应用)  回复  更多评论   

原来是要建立CLR应用程序才行,和同样的c++程序比,大小是小了许多,不过运行慢了许多
2008-08-14 12:22 | 郢都孤鬼

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理