分类目录归档:C#

在Unity Android 程序中使用动态库及注意事项

很多C/C++代码以动态库的方式供第三方调用,在Unity中,这类文件(dll for windows, *.so file for Android/Linux, *.dylib for MAC OSX)叫插件。

在Unity开发的Android程序中使用动态库插件是非常方便的,曾经因为被误导而放弃使用unity,转而研究Android通过原生Java的JNI方式包装*.so文件,虽然还行,但是JNI晦涩丑陋的API实在看着不舒服。相对而言,C#也是可以直接包装*.so文件并在Android系统中直接调用的,而且Unity制作Android app跟Unity制作其他平台的app是无缝的,只是在发布时选择要发布的平台就行了,真正做到了平台无关,极大的方便了开发。

我还没仔细研究如何在Android Studio中开发NDK,这里只介绍如何用Eclipse开发NDK。

在Eclipse中开发NDK的几个必须的安装项:NDK编译环境、Android SDK直接从官网单独下载单独安装;CDT、ADT插件可以直接从Eclipse的help的插件管理中安装。

然后就可以创建项目了:

1、打开Eclipse,通过File->New Project,弹出对话框,填入项目名称:

01

2、点Next,出现Config Project窗口:

02

由于只用于编译NDK,所以把前两项的钩钩去掉,第三个勾上,标记为so项目;目录也自定义一下,因为通常情况下我只会为编译so文件配置项目,真正的代码会单独放在更顶层的目录,方便跨平台的其他编译项目使用。结果如下:

03

4、按Finish结束创建过程,目录结构如下:

04

5、添加Native代码支持。也就是JNI相关的东西:在左侧项目根目录上右键->Android Tools->Add Native Support…

05

在随后弹出的对话框中输入要生成的so文件的名字:

06

这时候会发现多了一个目录:

07

6、编译配置

在jni目录中加入Application.mk文件和Android.mk文件

Application.mk:

APP_ABI      := all
#APP_ABI       := armeabi-v7a
#APP_ABI       += armeabi

#APP_OPTIM        := release
APP_PLATFORM     := android-8
#APP_BUILD_SCRIPT := Android.mk

# GNU STL implements most C++11 features. Use either gnustl_static or gnustl_shared
# Without this your C++ code will not be able to access headers like <thread>, <mutex>
#APP_STL := stlport_static
#APP_CPPFLAGS := -std=c++11 -frtti -fexceptions 
APP_STL       := gnustl_static
APP_CPPFLAGS  := -std=gnu++11 -pthread -frtti -fexceptions -DNDEBUG #-NDEBUG -mfpu=neon -fomit-frame-pointer -DULM_BLOCKED -msse3 -mfpmath=sse

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := PNLib
LOCAL_SRC_FILES := PNLib.cpp

include $(BUILD_SHARED_LIBRARY)

本来Android.mk文件很简单,把所有头文件、cpp文件加入,直接编译就行。但是对于我的项目这远远不够,因为我的代码要跨平台,有各个平台的编译项目单独出去,同时使用一份src文件,所以代码被放到了顶层目录中的src目录下,我需要遍历这个目录并把它加入NDK编译系统中来,所以下面从网上找了一段遍历头文件和cpp文件的脚本来用了:

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE   := PNLib

# 锟斤拷锟斤拷锟皆硷拷锟斤拷源锟侥硷拷目录锟斤拷源锟侥硷拷锟斤拷缀锟斤拷
MY_FILES_PATH  :=  $(LOCAL_PATH)/../../src

#$(warning $(MY_FILES_PATH))

MY_FILES_SUFFIX := %.cpp %.c

# 递归遍历目录下的所有的文件
rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

# 获取相应的源文件
MY_ALL_FILES := $(foreach src_path,$(MY_FILES_PATH), $(call rwildcard,$(src_path),*.*) ) 
MY_ALL_FILES := $(MY_ALL_FILES:$(MY_CPP_PATH)/./%=$(MY_CPP_PATH)%)
MY_SRC_LIST  := $(filter $(MY_FILES_SUFFIX),$(MY_ALL_FILES)) 
MY_SRC_LIST  := $(MY_SRC_LIST:$(LOCAL_PATH)/%=%)

# 去除字串的重复单词
define uniq =
  $(eval seen :=)
  $(foreach _,$1,$(if $(filter $_,${seen}),,$(eval seen += $_)))
  ${seen}
endef

# 递归遍历获取所有目录
MY_ALL_DIRS := $(dir $(foreach src_path,$(MY_FILES_PATH), $(call rwildcard,$(src_path),*/) ) )
MY_ALL_DIRS := $(call uniq,$(MY_ALL_DIRS))

# 赋值给NDK编译系统
LOCAL_SRC_FILES  := $(MY_SRC_LIST)
LOCAL_C_INCLUDES := $(MY_ALL_DIRS)

# Add additional include directories
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../
#LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../Eigen-3.2.2   
#必须从Android.mk配置文件中拿掉对Eigen的直接包含,放到程序代码中用相对路径包含:
# #include "../../Eigen-3.2.2/Eigen"
# using namespace Eigen;

#$(warning $(LOCAL_SRC_FILES))
#$(warning $(LOCAL_C_INCLUDES))

# use log system in NDK
LOCAL_LDLIBS += -llog

include $(BUILD_SHARED_LIBRARY)

7、最后一步是为此项目配置一个NDK编译工具:

A、项目->右键->Properties,弹出项目属性对话框:

08

B、选中Builders,点击New按钮,弹出框中选中“Program”,点击OK:

09

C、出现新Builder配置对话框,随便起个名字“New_Builder_for_NDK”;

在Main标签页中,“Location”项,通过“Browse File System…“找到NDK的build文件,windows系统为ndk-build.cmd,Mac或其他类Linux系统为ndk-build;

工作目录”Working Directory“指定当前项目下的jni目录就行了。

10

D、切到Refresh页,勾选”Refresh resources upon completion“

11

E、切到”Build Options“页,勾选”Specify working set of relevant resources“,点击后面按钮”Specify Resources…“,指本项目下的jni目录。

12

最后的编译条如下:

13

点击左边锤子图标即可对项目进行编译。剩下的工作就是一步步修正跨平台代码,最后生成PNLib.so文件。

 

以上步骤有几个地方需要特别注意:

1、使用gun++11

NKD支持c++11,GCC已经支持大部分c++11特性,所以可以直接使用gun++11。实际上,如果用c++11,Android版程序可能报错:DLLNotFundException,编译出来的动态库在Android系统中无法加载,所以Application.mk文件中必须如下配置:

APP_STL       := gnustl_static
APP_CPPFLAGS  := -std=gnu++11 -pthread -frtti -fexceptions -DNDEBUG

2、x64版ndk目前还有bug

你的系统即便是x64的,也别下载x64版本的ndk,否则会报找不到make.exe的问题(make.exe不是内部命令XXXXXXXXX)

 

3、不识别vector、list等模板类的问题

Add

APP_STL := stlport_static

to their Application.mk file.

其实Android插件应该用APP_STL := gnustl_static

 

4、使用第三方库Eigen导致array等冲突的问题

原因是在NDK的Android.mk文件中把Eigen的路径也加入到里面了。

正确的做法是,不在Android.mk文件加入Eigen,而是在程序代码中直接包含Eigen头文件。

 

Unity 5.0 中使用C/C++开发的dll插件使用中的问题 DllNotFoundException 问题集

Unity 中会经常用到C/C++开发的动态库(dll),Unity称为第三方插件,或plugin,这是非常厉害的扩展方法,但是Unity使用dll也非常痛苦,错误百出。

最常见的问题是 DllNotFoundException,就是找不到动态库。导致DllNotFoundException的原因很多,现把一些可能的情况,以及其解决方案一一列举:

1、DllNotFoundException: SerialPortDevice.dll
demo.Start () (at Assets/demo.cs:39)

Unity基于Mono实现的跨平台特性,但是Mono的 .net framework只充分实现了.net 2.0 subet,至于.net framework 4.5,照目前的速度,估计要等到下辈子了。

操作串口有很多问题,于是我自己用C++分装了一个dll,用于管理串口。从dll中导出的C#代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

// Wrapper of library by C#
namespace PluginImport
{
	#region delegates
	public delegate void ReceivedDataCallback(IntPtr serialPortRef, ref byte[] data, int len);
	//
	public delegate void SerialPortStatusChanged(IntPtr serialPortRef, SerialPortStatus status, string msg);
	#endregion
	
	#region Data types	
	// Serial Port Status
	public enum SerialPortStatus
	{
		PortClosed,              // 端口已关闭
		PortOpened,              // 端口已打开
		PortError,               // 端口错误
		PortInvalid,             // 端口不可用
	};
	
	// SerialPort Identity	
	public struct SerialPortRef
	{
		public IntPtr Identity;
	};
	
	#endregion
	
	#region API Wraper
	public class SerialPortDevice
	{
		// Create a serial port for lately operations. return Identity of new serial port.
		// Return NULL if create failed.
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		[return: MarshalAs(UnmanagedType.Struct)]
		public static extern SerialPortRef CreateSerialPort();

		// Register handl to receive data from a SerialPort.
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern void RegisterReceivedDataCallback(SerialPortRef sp, ReceivedDataCallback handle);
		
		// Register handl to receive serial port status changed event.
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern void RegisterSerialPortStatusChangedCallback(SerialPortRef sp, SerialPortStatusChanged handle);
		
		// Open a serial port by name.
		// Return false if open failed.
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern bool OpenSerialPort(SerialPortRef sp, string portName, int bauntRate);
		
		// Write and send data by serial port
		// Return the length sent by serial port
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern int SerialPortWrite(SerialPortRef sp, byte[] data, int len);
		
		// Read data from serial port
		// Return the length readed from serial port
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern int SerialPortRead(SerialPortRef sp, ref byte[] buffer, int bufferLen);
		
		// Check wether a serail port is openning.
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern bool IsSerialPortOpenning(SerialPortRef sp);
		
		// Close a serial port.
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern void CloseSerialPort(SerialPortRef sp);
		
		// Destroy a serial port.
		[DllImport("SerialPortDevice.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		public static extern void DestroySerialPort(SerialPortRef sp);
	};
	#endregion
}

然后把dll拷贝到Unit Prj 的assert目录下,报错:

DllNotFoundException: SerialPortDevice.dll
demo.Start () (at Assets/demo.cs:39)

折腾了很久,发现在导入dll根本不需要后缀“.dll”,即写上dll文件名就OK了,不需要.dll后缀:

		[DllImport("SerialPortDevice", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, ExactSpelling = true)]
		[return: MarshalAs(UnmanagedType.Struct)]

但是这是VS的C#项目的默认做法。一个习惯性的动作导致了这个问题。于是把所有“.dll”删除,OK了。

 

2、Failed to load ‘Assets/PNLib.dll’ with error ‘The specified module could not be found.‘, GetDllDirectory returned ”. If GetDllDirectory returned non empty path, check that you’re using SetDirectoryDll correctly.

这种情况是,找到dll了,但是无法装载。看提示基本还是一头雾水: ‘The specified module could not be found.‘, GetDllDirectory returned ”。看似还是路径的问题。

这个问题测试了很多种情况,基本上是,只要是dll放到Assets目录下(或Assets下创建的Plugin目录下)就都会提示这个错误。而放到Assets外与Assets同目录就OK了,这难道不是Unity的某个bug吗?更奇怪的是之前在Assets下的Plugin里放的另一个dll(SerialPortDevice.dll)就不会有这样的问题。何解?

 

如果还有其他问题,后续继续补充。。。。。。

 

Returning Strings from a C++ API to C#

从C++的dll中返回字符串是一种经常用到的C#调用C++函数的方式,但是带有返回值的调用一直是说不清道不明的问题,不细细研究,的确没法用起来。

这里直接给出一个正确的做法,后面会提供一份Google出来的资料,讲的非常好。原理知道了,对于返回结构体也应该是可以按同理搞定的。

 

Code in C++ dll:

// If any error, you can call 'BRGetLastErrorMessage' to get error information
BDR_API char* BRGetLastErrorMessage()
{
    return Error::Instance()->GetLastError();

    // follow code is not platform-cross, so throw away.

#ifdef __OS_XUN__
    return Error::Instance()->GetLastError();
#else
    char* msg = Error::Instance()->GetLastError();
    int len = strlen(msg);

    char* pszReturn = (char*)::CoTaskMemAlloc(len);
    // Copy the contents of szSampleString
    // to the memory pointed to by pszReturn.
    strcpy(pszReturn, msg);
    // Return pszReturn.
    return pszReturn;
#endif
}

 

C# side code:

        // If any error, you can call 'BRGetLastErrorMessage' to get error information
        [DllImport("BVHDataReader.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        //[return: MarshalAs(UnmanagedType.LPStr)]
        private static extern IntPtr BRGetLastErrorMessage();
        public static string strBRGetLastErrorMessage()
        {
            // Receive the pointer to Unicde character array
            // from API.
            IntPtr ptr = BRGetLastErrorMessage();
            // Construct a string from the pointer.
            string str = Marshal.PtrToStringAnsi(ptr);
            // Display the string.
            Console.WriteLine("Returned string : " + str);
            return str;
        }

 

 

下面是同名资料:

Returning Strings from a C++ API to C#

1. Introduction.

1.1 APIs that return strings are very common. However, the internal nature of such APIs, as well as the use of such APIs in managed code, require special attention. This blog will demonstrate both concerns.

1.2 I will present several techniques for returning an unmanaged string to managed code. But before that I shall first provide an in-depth explanation on the low-level activities that goes on behind the scenes. This will pave the way towards easier understanding of the codes presented later in this blog.

2. Behind the Scenes.

2.1 Let’s say we want to declare and use an API written in C++ with the following signature :

char* __stdcall StringReturnAPI01();

This API is to simply return a NULL-terminated character array (a C string).

2.2 To start with, note that a C string has no direct representation in managed code. Hence we simply cannot return a C string and expect the CLR to be able to transform it into a managed string.

2.3 The managed string is non-blittable. It can have several representations in unmanaged code : e.g. C-style strings (ANSI and Unicode-based) and BSTRs. Hence, it is important that you specify this information in the declaration of the unmanaged API, e.g. :

[DllImport("<path to DLL>", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string StringReturnAPI01();

In the above declaration, note that the following line :

[return: MarshalAs(UnmanagedType.LPStr)]

indicates that the return value from the API is to be treated as a NULL-terminated ANSI character array (i.e. a typical C-style string).

2.4 Now this unmanaged C-style string return value will then be used by the CLR to create a managed string object. This is likely achieved by using theMarshal.PtrToStringAnsi() method with the incoming string pointer treated as an IntPtr.

2.5 Now a very important concept which is part and parcel of the whole API calling operation is memory ownership. This is an important concept because it determines who is responsible for the deallocation of this memory. Now the StringReturnAPI01() API supposedly returns a string. The string should thus be considered equivalent to an “out” parameter, It is owned by the receiver of the string, i.e. the C# client code. More precisely, it is the CLR’s Interop Marshaler that is the actual receiver.

2.6 Now being the owner of the returned string, the Interop Marshaler is at liberty to free the memory associated with the string. This is precisely what will happen. When the Interop Marshaler has used the returned string to construct a managed string object, the NULL-terminated ANSI character array pointed to by the returned character pointer will be deallocated.

2.7 Hence it is very important to note the general protocol : the unmanaged code will allocate the memory for the string and the managed side will deallocate it. This is the same basic requirement of “out” parameters.

2.8 Towards this protocol, there are 2 basic ways that memory for an unmanaged string can be allocated (in unmanaged code) and then automatically deallocated by the CLR (more specifically, the interop marshaler) :

  • CoTaskMemAlloc()/Marshal.FreeCoTaskMem().
  • SysAllocString/Marshal.FreeBSTR().

Hence if the unmanaged side used CoTaskMemAlloc() to allocate the string memory, the CLR will use the Marshal.FreeCoTaskMem() method to free this memory.

The SysAllocString/Marshal.FreeBSTR() pair will only be used if the return type is specified as being a BSTR. This is not relevant to the example given in point 2.1 above. I will demonstrate a use of this pair in section 5 later.

2.9 N.B. : Note that the unmanaged side must not use the “new” keyword or the “malloc()” C function to allocate memory. The Interop Marshaler will not be able to free the memory in these situations. This is because the “new” keyword is compiler dependent and the “malloc” function is C-library dependent. CoTaskMemAlloc(), and SysAllocString() on the other hand, are Windows APIs which are standard.

Another important note is that although GlobalAlloc() is also a standard Windows API and it has a counterpart managed freeing method (i.e. Marshal.FreeHGlobal()), the Interop Marshaler will only use the Marshal.FreeCoTaskMem() method for automatic memory freeing of NULL-terminated strings allocated in unmanaged code. Hence do not use GlobalAlloc() unless you intend to free the allocated memory by hand using Marshal.FreeHGlobal() (an example of this is give in section 6 below).

3. Sample Code.

3.1 In this section, based on the principles presented in section 2, I shall present sample codes to demonstrate how to return a string from an unmanaged API and how to declare such an API in managed code.

3.2 The following is a listing of the C++ function which uses CoTaskMemAlloc() :

extern "C" __declspec(dllexport) char*  __stdcall StringReturnAPI01()
{
    char szSampleString[] = "Hello World";
    ULONG ulSize = strlen(szSampleString) + sizeof(char);
    char* pszReturn = NULL;

    pszReturn = (char*)::CoTaskMemAlloc(ulSize);
    // Copy the contents of szSampleString
    // to the memory pointed to by pszReturn.
    strcpy(pszReturn, szSampleString);
    // Return pszReturn.
    return pszReturn;
}

3.4 The C# declaration and sample call :

[DllImport("<path to DLL>", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string StringReturnAPI01();

static void CallUsingStringAsReturnValue()
{
  string strReturn01 = StringReturnAPI01();
  Console.WriteLine("Returned string : " + strReturn01);
}

3.5 Note the argument used for the MarshalAsAttribute : UnmanagedType.LPStr. This indicates to the Interop Marshaler that the return string from StringReturnAPI01() is a pointer to a NULL-terminated ANSI character array.

3.6 What happens under the covers is that the Interop Marshaler uses this pointer to construct a managed string. It likely uses the Marshal.PtrToStringAnsi() method to perform this. The Interop Marshaler will then use the Marshal.FreeCoTaskMem() method to free the character array.

4. Using a BSTR.

4.1 In this section, I shall demonstrate here how to allocate a BSTR in unmanaged code and return it in managed code together with memory deallocation.

4.2 Here is a sample C++ code listing :

extern "C" __declspec(dllexport) BSTR  __stdcall StringReturnAPI02()
{
  return ::SysAllocString((const OLECHAR*)L"Hello World");
}

4.3 And the C# declaration and usage :

[DllImport("<path to DLL>", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.BStr)]
public static extern string StringReturnAPI02();

static void CallUsingBSTRAsReturnValue()
{
  string strReturn = StringReturnAPI02();
  Console.WriteLine("Returned string : " + strReturn);
}

Note the argument used for the MarshalAsAttribute : UnmanagedType.BStr. This indicates to the Interop Marshaler that the return string from StringReturnAPI02() is a BSTR.

4.4 The Interop Marshaler then uses the returned BSTR to construct a managed string. It likely uses the Marshal.PtrToStringBSTR() method to perform this. The Interop Marshaler will then use the Marshal.FreeBSTR() method to free the BSTR.

5. Unicode Strings.

5.1 Unicode strings can be returned easily too as the following sample code will demonstrate.

5.2 Here is a sample C++ code listing :

extern "C" __declspec(dllexport) wchar_t*  __stdcall StringReturnAPI03()
{
  // Declare a sample wide character string.
  wchar_t  wszSampleString[] = L"Hello World";
  ULONG  ulSize = (wcslen(wszSampleString) * sizeof(wchar_t)) + sizeof(wchar_t);
  wchar_t* pwszReturn = NULL;

  pwszReturn = (wchar_t*)::CoTaskMemAlloc(ulSize);
  // Copy the contents of wszSampleString
  // to the memory pointed to by pwszReturn.
  wcscpy(pwszReturn, wszSampleString);
  // Return pwszReturn.
  return pwszReturn;
}

5.3 And the C# declaration and usage :

[DllImport("<path to DLL>", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string StringReturnAPI03();

static void CallUsingWideStringAsReturnValue()
{
  string strReturn = StringReturnAPI03();
  Console.WriteLine("Returned string : " + strReturn);
}

The fact that a wide charactered string is now returned requires the use of the UnmanagedType.LPWStr argument for the MarshalAsAttribute.

5.4 The Interop Marshaler uses the returned wide-charactered string to construct a managed string. It likely uses the Marshal.PtrToStringUni() method to perform this. The Interop Marshaler will then use the Marshal.FreeCoTaskMem() method to free the wide-charactered string.

6. Low-Level Handling Sample 1.

6.1 In this section, I shall present some code that will hopefully cement the reader’s understanding of the low-level activities that had been explained in section 2 above.

6.2 Instead of using the Interop Marshaler to perform the marshaling and automatic memory deallocation, I shall demonstrate how this can be done by hand in managed code.

6.3 I shall use a new API which resembles the StringReturnAPI01() API which returns a NULL-terminated ANSI character array :

extern "C" __declspec(dllexport) char*  __stdcall PtrReturnAPI01()
{
  char   szSampleString[] = "Hello World";
  ULONG  ulSize = strlen(szSampleString) + sizeof(char);
  char*  pszReturn = NULL;

  pszReturn = (char*)::GlobalAlloc(GMEM_FIXED, ulSize);
  // Copy the contents of szSampleString
  // to the memory pointed to by pszReturn.
  strcpy(pszReturn, szSampleString);
  // Return pszReturn.
  return pszReturn;
}

6.4 And the C# declaration :

[DllImport("<path to DLL>", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr PtrReturnAPI01();

Note that this time, I have indicated that the return value is an IntPtr. There is no [return : …] declaration and so no unmarshaling will be performed by the Interop Marshaler.

6.5 And the C# low-level call :

static void CallUsingLowLevelStringManagement()
{
  // Receive the pointer to ANSI character array
  // from API.
  IntPtr pStr = PtrReturnAPI01();
  // Construct a string from the pointer.
  string str = Marshal.PtrToStringAnsi(pStr);
  // Free the memory pointed to by the pointer.
  Marshal.FreeHGlobal(pStr);
  pStr = IntPtr.Zero;
  // Display the string.
  Console.WriteLine("Returned string : " + str);
}

This code demonstrates an emulation of the Interop Marshaler in unmarshaling a NULL-terminated ANSI string. The returned pointer from PtrReturnAPI01() is used to construct a managed string. The pointer is then freed. The managed string remains intact with a copy of the returned string.

The only difference between this code and the actual one by the Interop Marshaler is that the GlobalAlloc()/Marshal.FreeHGlobal() pair is used. The Interop Marshaler always uses Marshal.FreeCoTaskMem() and expects the unmanaged code to use ::CoTaskMemAlloc().

7. Low-Level Handling Sample 2.

7.1 In this final section, I shall present one more low-level string handling technique similar to the one presented in section 6 above.

7.2 Again we do not use the Interop Marshaler to perform the marshaling and memory deallocation. Additionally, we will also not release the memory of the returned string.

7.3 I shall use a new API which simply returns a NULL-terminated Unicode character array which has been allocated in a global unmanaged memory :

wchar_t gwszSampleString[] = L"Global Hello World";

extern "C" __declspec(dllexport) wchar_t*  __stdcall PtrReturnAPI02()
{
  return gwszSampleString;
}

This API returns a pointer to the pre-allocated global Unicode string “gwszSampleString”. Because it is allocated in global memory and may be shared by various functions in the DLL, it is crucial that it is not deleted.

7.4 The C# declaration for PtrReturnAPI02() is listed below :

[DllImport("<path to DLL>", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr PtrReturnAPI02();

Again, there is no declaration for interop marshaling (no use of the [return : …] declaration). The returned IntPtr is returned as is.

7.5 And a sample C# code to manage the returned IntPtr :

static void CallUsingLowLevelStringManagement02()
{
  // Receive the pointer to Unicde character array
  // from API.
  IntPtr pStr = PtrReturnAPI02();
  // Construct a string from the pointer.
  string str = Marshal.PtrToStringUni(pStr);
  // Display the string.
  Console.WriteLine("Returned string : " + str);
}

Here, the returned IntPtr is used to construct a managed string from an unmanaged NULL-terminated Unicode string. The memory of the unmanaged Unicode string is then left alone and is not deleted.

Note that because a mere IntPtr is returned, there is no way to know whether the returned string is ANSI or Unicode. In fact, there is no way to know whether the IntPtr actually points to a NULL-terminated string at all. This knowledge has to be known in advance.

7.6 Furthermore, the returned IntPtr must not point to some temporary string location (e.g. one allocated on the stack). If this was so, the temporary string may be deleted once the API returns. The following is an example :

extern "C" __declspec(dllexport) char* __stdcall PtrReturnAPI03()
{
  char szSampleString[] = "Hello World";
  return szSampleString;
}

By the time this API returns, the string contained in “szSampleString” may be completely wiped out or be filled with random data. The random data may not contain any NULL character until many bytes later. A crash may ensue a C# call like the following :

IntPtr pStr = PtrReturnAPI03();
// Construct a string from the pointer.
string str = Marshal.PtrToStringAnsi(pStr);

Link:http://limbioliong.wordpress.com/2011/06/16/returning-strings-from-a-c-api/

 

How can I return a string from a C dll to C#?

// c code:
#include <ole2.h> /* needed for CoTaskMemAlloc */
EXPORT char* getUrlFromBaseURLInList(char *ListFilePath, char *SrchBaseURL, char* strBuffer)
{
    static const char[] sTest =  "Test My String";
    /* you must use CoTaskMemAlloc to allocate the memory, not malloc, new, or anything else */
    char* returnedString = CoTaskMemAlloc(sizeof(sTest));
    strcpy(returnedString, sTest);
    return returnedString;
}

// c# code:
[DllImport("SLSSeoUrlFunc.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
public static extern string getUrlFromBaseURLInList(byte[] ListFilePath, byte[] sSrchUrl);

string myTestString = SLSSeoUrlFuncDllWrap.getUrlFromBaseURLInList(null, null);
Console.WriteLine("Eureka - {0}", myTestString);

在C#中调试C++的DLL

“我的小伙伴们都惊呆了!”

今天才发现,其实C#是可以跟进C++代码的!!!在VS2012里的做法是:

在C#项目上右键点击,在右键菜单上选择“属性” –> 调试–>启用调试器–>勾选“启用本机代码调试(T)”

这样之后,就可以直接在C++项目里的C++代码上直接打断点调试了!

如下图所示:

1、在项目上右键,然后在右键菜单中选择属性

2、勾选启用本机代码调试

能调试到C++代码,还有啥好担心的??? :)

 

 

 

 

四元数及其平滑处理

最近做camera 的 AI,需要对四元数,欧拉角等要有一定的了解,把前面学习的整理了一下:

1。四元数的优势

 三维空间的旋转完全可以由4元数来胜任。传统意义上需要3×3矩阵来进行向量的旋转(4×4矩阵的第四列表示平移)。所以四元数更节省空间,运算速度更快。既然四元数能方便的表示3D旋转,那么对他们进行插值就能产生平滑的旋转效果。

劣势可能是比较抽象,不大好理解。而且据说顶点变换还是矩阵效率更高(涉及到平移)。

2。四元数的物理意义

Q( x, y, z, w)来表示向量 绕轴 A(ax, ay,az) 旋转alpha

则: x = sin(alpha/2)*ax;

y =   sin(alpha/2)*ay;

z =  sin(alpha/2)*az;

w = cos(alpha/2);

 

3。四元素的数学意义

我们知道 复数的表示为 a+bi; 其中i*i = -1;

四元数 q = w + xi + yi +zi;

复数的运算法则为四元数的数学运算提供了规则基础。比如说乘法,加法等。

4。四元数和矩阵间的转换

QX =0; QY=1;QZ=2;QW=3;

void quat_ConvertFromMatrix(float *pQuat, const float mat[4][4])
{
 float diag, s;
 int i, j, k;

 diag = mat[0][0] + mat[1][1] + mat[2][2];

 if(diag < -0.999f )
 {
  i = QX;
  if( mat[QY][QY] > mat[QX][QX] )
   i = QY;
  if( mat[QZ][QZ] > mat[i][i] )
   i = QZ;

  j = g_QNext[i];
  k = g_QNext[j];

  s = ltsqrtf( mat[i][i] - ( mat[j][j] + mat[k][k] ) + /*mat[3][3]*/ 1.0f );

  pQuat[i] = s * 0.5f;
  s = 0.5f / s;
  pQuat[QW] = ( mat[k][j] - mat[j][k] ) * s;
  pQuat[j] = ( mat[j][i] + mat[i][j] ) * s;
  pQuat[k] = ( mat[k][i] + mat[i][k] ) * s;
  return;
 }

 s = ltsqrtf( diag + /*mat[3][3]*/ 1.0f );

 pQuat[3] = s * 0.5f;
 s = 0.5f / s;

 pQuat[0] = (mat[2][1] - mat[1][2]) * s;
 pQuat[1] = (mat[0][2] - mat[2][0]) * s;
 pQuat[2] = (mat[1][0] - mat[0][1]) * s;
}

void quat_ConvertToMatrix(const float *pQuat, float mat[4][4])
{
 float s, xs, ys, zs, wx, wy, wz, xx, xy, xz, yy, yz, zz;

/*!
  get the values for matrix calcuation.
*/
 s = 2.0f / ((pQuat[0] * pQuat[0]) + (pQuat[1] * pQuat[1]) + 
  (pQuat[2] * pQuat[2]) + (pQuat[3] * pQuat[3]));

 xs = pQuat[0] * s;
 ys = pQuat[1] * s;
 zs = pQuat[2] * s;

 wx = pQuat[3] * xs;
 wy = pQuat[3] * ys;
 wz = pQuat[3] * zs;

 xx = pQuat[0] * xs;
 xy = pQuat[0] * ys;
 xz = pQuat[0] * zs;

 yy = pQuat[1] * ys;
 yz = pQuat[1] * zs;

 zz = pQuat[2] * zs;

/*!
  Fill in matrix

*/
 mat[0][0] = 1.0f - (yy + zz);
 mat[0][1] = xy - wz;
 mat[0][2] = xz + wy;

 mat[1][0] = xy + wz;
 mat[1][1] = 1.0f - (xx + zz);
 mat[1][2] = yz - wx;

 mat[2][0] = xz - wy;
 mat[2][1] = yz + wx;
 mat[2][2] = 1.0f - (xx + yy);

 mat[0][3] = mat[1][3] = mat[2][3] = mat[3][0] = mat[3][1] = mat[3][2] = 0.0f;
 mat[3][3] = 1.0f;
}

 

具体推倒过程可以参考相关文献。

5。四元数插值

有很多插值方法,比如线性,球形,样条插值等;

lerp (t;,q0,q1) = (1-t)q0 + tq1 ;//快速,但动画不平滑,需要归一化 / ||(1-t)q0 + tq1||;

slerp( t;, q0,q1) = [q0 *sin(thata(1-thata)) + q1sin(thata*t)] / sin(thata); //平滑,归一化;

thata 为q0 q1夹角。q0 dot q1 = cos(thata);

相关代码:

void quat_Slerp(float *pDest, const float *pQ1, const float *pQ2, float t)
{
 float rot1q[4];
 float omega, cosom, oosinom;
 float scalerot0, scalerot1;

/*!
  Calculate the cosine

*/
 cosom = pQ1[0]*pQ2[0] + pQ1[1]*pQ2[1] + pQ1[2]*pQ2[2] + pQ1[3]*pQ2[3];

/*!
  adjust signs if necessary

*/
 if(cosom < 0.0f)
 {
  cosom = -cosom;
  rot1q[0] = -pQ2[0];
  rot1q[1] = -pQ2[1];
  rot1q[2] = -pQ2[2];
  rot1q[3] = -pQ2[3];
 }
 else  
 {
  rot1q[0] = pQ2[0];
  rot1q[1] = pQ2[1];
  rot1q[2] = pQ2[2];
  rot1q[3] = pQ2[3];
 }

/*!
  calculate interpolating coeffs

*/
 if ( (1.0f - cosom) > 0.0001f ) 
 { 
/*!
   standard case

*/
  omega   = ltacosf(cosom);
  oosinom = 1.0f / ltsinf(omega);
  scalerot0 = ltsinf((1.f - t) * omega) * oosinom;
  scalerot1 = ltsinf(t * omega) * oosinom;
 }
 else
 { 
/*!
   rot0 and rot1 very close - just do linear interp.

*/
  scalerot0 = 1.0f - t;
  scalerot1 = t;
 }

 //! build the new quarternion
 pDest[0] = (scalerot0 * pQ1[0] + scalerot1 * rot1q[0]);
 pDest[1] = (scalerot0 * pQ1[1] + scalerot1 * rot1q[1]);
 pDest[2] = (scalerot0 * pQ1[2] + scalerot1 * rot1q[2]);
 pDest[3] = (scalerot0 * pQ1[3] + scalerot1 * rot1q[3]);
}

 

参考文献:

游戏编程精粹

 

本文转自:http://blog.csdn.net/hziee_/article/details/1630116

 

 

 

C++通过Callback向C#传递数据

现在比较流行C#与C++融合:C#做GUI,开发效率高,C++做运算,运行效率高,二者兼得。

但是C++与C#必然存在数据交互,C#与C++dll的数据交互从来都是一个让人头疼的问题。

从调用方式看也有两种情况:

1、C#调用C++函数

这种情况用的比较多,数据流向可以是C#流向C++,通过参数将数据传递给C++(如:SetData(double[] data));也可以是C++流向C#(如:GetData(double[] data))。

2、C++ Callback

这种情况是C++中通过Callback的方式调用C#代码,类似于C++做过一些处理后向C#发送事件,事件可以携带数据(如处理后的数据)。则C++中定义函数指针的方式是:

typedef  void(*Render)(double* data, BOOL* color);

 

C#作为委托,定义的函数被C++ callback:

public delegate void RenderCallback([MarshalAs(UnmanagedType.LPArray, SizeConst =23)]double[] data, [MarshalAs(UnmanagedType.LPArray, SizeConst = 23)]int[] colors);

千万注意,delegate中的double[]数组一定要加上MarshalAs标记,标记为传递数组,而且必须指定传递的数量,如果不标记数量,则每次只传递一个数值,这个问题折磨我很久才搞定!

其他注意事项:

1、如何在C#中保持C++的函数指针

回调函数的另一个注意事项是向C++ dll传递回调函数指针的问题

假设有个函数向C++dll传递指针:

    public delegate void EKFRenderCallback(string data, string colors);

    public class EKFLib
    {
        [DllImport("EKFLib.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern void SetRenderCallback(EKFRenderCallback render);

C#中如下传递被回调的函数:

        public void RenderCallback(string data, string color)
        {
            // rendering
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            EKFLib.SetRenderCallback(RenderCallback);
            EKFLib.Init();
        }

这虽然没什么问题,但是通过SetRenderCallback()传入到C++的指针不受托管代码管理,在C#中认为此指针对象未被任何代码引用,GC做垃圾回收时,将会把C#本地的空指针回收,导致C++无法执行回调,出现“CallbackOnCollectedDelegate”错误:

对“MotionCapture!MotionCapture.EKFRenderCallback::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

微软官网的例子是控制GC回收机制,这是个比较笨拙的方法,更加理所当然的方法是把委托定义成一个属性,指向一个new出来的callback,然后再把这个callback传递进C++dll中,这样,在C#端有对象引用,保证了GC不会回收此callback:

        public void RenderCallback(string data, string color)
        {
            // rendering
        }

        private EKFRenderCallback render;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            render = new EKFRenderCallback(RenderCallback);
            EKFLib.SetRenderCallback(render);
            EKFLib.Init();
        }

 

2、__stdcall与_cdecl传递数据

最近一个项目是通过C++ 的 dll做高速运算,然后把结果数据通过Callback的方式回调给C#(界面部分),结果总是在C#中接到回调事件后就直接挂掉(程序直接在毫无提示的情况下退出,没有任何调试信息或者提示)。

导致问题的原因是,默认情况下,C++中如下定义的函数指针,默认是以_cdecl方式调用的:

typedef  void(*Render)(double* data, BOOL* color);

这种情况下,参数堆栈是由调用者(C++一侧)维护的,在C++调用此回调函数后,会把参数弹出堆栈而释放,导致C#读取数据时出现莫名其妙的错误。

以上是回调函数传递数组可能出现的情况,而如下所示,只传递一个参数的情况,甚至会在C#方莫名其妙的卡死:

typedef void (*CalibrationProgressCallback)(double percent);
改为__stdcall的方式即可解决问题,申明如下:

typedef  void(__stdcall *Render)(double* data, BOOL* color);

以下来自网络的一段_cdecl和__stdcall的解释,必须牢记:

1. __cdecl

即所谓的C调用规则,按从右至左的顺序压参数入栈,由调用者把参数弹出栈。切记:对于传送参数的内存栈是由调用者来维护的。返回值在EAX中。因此,对于象printf这样变参数的函数必须用这种规则。编译器在编译的时候对这种调用规则的函数生成修饰名的饿时候,仅在输出函数名前加上一个下划线前缀,格式为_functionname。
2. __stdcall

按从右至左的顺序压参数入栈,由被调用者把参数弹出栈。_stdcall是Pascal程序的缺省调用方式,通常用于Win32 Api中,切记:函数自己在退出时清空堆栈,返回值在EAX中。  __stdcall调用约定在输出函数名前加上一个下划线前缀,后面加上一个“@”符号和其参数的字节数,格式为_functionname@number。如函数int func(int a, double b)的修饰名是_func@12

所以,从C++ dll中回调函数给C#传递数据,必须由C#函数在使用完数据后(退出函数时)自己清空堆栈!所C++中的回调函数指针应该如下定义:

typedef void (_stdcall *CalibrationProgressCallback)(double percent);

总结:

C++通过callback向C#传递数据必须注意以下几点:

1、C++中的回调函数必须用_stdcall标记,使用stdcall方式回调;

2、如果是数组,必须用 [MarshalAs(UnmanagedType.LPArray, SizeConst = 23)]标记参数,指定为数组且标记数组长度;

3、C#方必须申明一个变量,用来指向C++的回调指针函数,避免被C#回收掉。

 

 

 

 

 

C# import dll的P/Invoke调用在性能上的损失

    Net Framework提供了P/Invoke方式从托管代码中调用非托管的本地Dll,但是Micorsoft表示P/Invoke的性能并不是很好,远远达不到本地代码调用本地代码Dll的性能,所谓P/Invoke的性能不好并非是本地代码的执行效率问题,P/Invoke不会影响到非托管代码的执行,只是调用过程中的开销比较大,也就是说高密度频繁调用小量的高效率代码的代价可能会超过纯粹的托管代码,其效率损耗主要在托管类型与本地类型的相互转换上,以及非托管Dll的加载与释放上,所以使用P/Invoke非常适合足够规模一整块的高效率代码调用,而非频繁的小规模调用,小规模高密度频繁的P/Invoke调用往往在性能上得不偿失。

C#调用C/C++动态库必须注意的几个问题

经常需要封装一些C/C++函数放入动态库中给C#程序调用,通常情况下直接写成如下形式即可:

C#封装调用:

[DllImport("depressor.dll")]
private static extern void DataDepress(ushort[] data);

但是经常出现找不到函数、出现PInvoke错误、传入的数据是一堆莫名其妙的数值之类的错误。

实际上,DLLImport的几个参数非常非常重要,这些值的设置直接影响到了是否能够找到函数、函数调用时的压栈方式、参数传入的是否正确有直接的关系。

以下几个问题必须注意:

1、dll中的函数前必须加入extern “C”,否则会按照C++方式编译,函数会是ZxA+函数名之类的函数名称,导致找不到函数的错误;

2、在C#中做函数封装时,函数必须加入CallingConvention = CallingConvention.Cdecl标签,按照C格式调用,否则会报PInvoke错误;

3、在C#中做函数封装时,函数必须加入CharSet = CharSet.Ansi标签,保证参数以Ansi方式传入,否则传入地址混乱!!!

举例:

C++ Dll函数:

 extern "C" DEPRESSOR_API void DataDepress(ushort* cprs);

C#封装调用:

[DllImport("depressor.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern void DataDepress(ushort[] data);

4、未处理DllNotFoundException:无法加载 DLL“EKFLib.dll”: 内存位置访问无效。 (异常来自 HRESULT:0x800703E6)

这通常是因为dll中直接给空指针赋值等C++ dll中的bug导致的问题。

因为我刚刚解决了一个bug:
C++dll中的一个对象构造函数时调用了一个回调函数,此时回调函数还是个未赋值的指针,所以出现内存位置访问无效的错误,但C#中提示的是:

未处理DllNotFoundException
无法加载 DLL“EKFLib.dll”: 内存位置访问无效。 (异常来自 HRESULT:0x800703E6)。

不要被DllNotFoundException误导了。

C#中利用联合类型(union)做数值类型转换(byte[] to float)

在C/C++中,联合类型经常用来做数据类型转换,比如下面的联合类型可以直接将char数组转换成float类型:

#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

typedef union _UnFloat
{
float value;
char buff[4];
}UnFloat;
int main()
{
// convertor
UnFloat uf;
uf.value = 0;

float f = 3.1415626;

// byte array
char cf[4];
memset(cf, '\0', sizeof(cf));
memcpy(cf, &f, sizeof(cf));

// convert
memcpy(uf.buff, cf, sizeof(uf.buff));

// output
printf("The union converted float value is %f", uf.value);

char input[255];
gets(input);

return 0;
}

输出为:

The union converted float value is 3.141563

C#中没有联合类型(union),但是利用FieldOffset属性,就可以通过结构体定义联合类型:

using System.Runtime.InteropServices;

namespace FloatConvert
{
[StructLayout(LayoutKind.Explicit)]
public struct UnionFloat
{
[FieldOffset(0)]
public float value;
[FieldOffset(0)]
public byte buff0;
[FieldOffset(1)]
public byte buff1;
[FieldOffset(2)]
public byte buff2;
[FieldOffset(3)]
public byte buff3;
}

}

C# byte[] 获取数组的IntPtr

有几种方法可以访问与字节数组相对应的 IntPtr。

第一种是使用不安全的代码块来访问直接指向字节数组的指针。

[code lang=”Cpp”]
//C#
unsafe
{
byte[] test = new byte[5];
fixed (byte* p = &test[0])
{
*p = 0xff;
}
}
[/code]

也可以使用 GCHandle 来获得对象。

[code lang=”Cpp”]
//C#

using System.Runtime.InteropServices;

byte[] test = new byte[5];
GCHandle hObject = GCHandle.Alloc(test, GCHandleType.Pinned);
IntPtr pObject = hObject.AddrOfPinnedObject();

if(hObject.IsAllocated)
hObject.Free();
[/code]

最后,可以这样实现:通过 LocalAlloc 创建内存块并将数据封送处理到该内存块。

[code lang=”Cpp”]
//C#

[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalAlloc(uint uFlags, uint uBytes);

[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalFree(IntPtr hMem);

[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalReAlloc(IntPtr hMem, uint uBytes, uint fuFlags);

public const uint LMEM_FIXED = 0;
public const uint LMEM_MOVEABLE = 2;
public const uint LMEM_ZEROINIT = 0x0040;

byte[] test = new byte[5];
IntPtr p = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, (uint)test.Length);

if (p == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
else
{
Marshal.Copy(test, 0, p, test.Length);
}
[/code]

本文转自:http://hi.baidu.com/lilipangtou/blog/item/5749bb211d97fcebd7cae2b5.html

WPF中的Timer与DispatcherTimer的区别与应用

WPF / Silverlight中的 Timer 与 DispatcherTimer 有什么区别呢?

这里我给大家简单介绍一下他们在使用和实现上的区别。

在一个应用程序中,Timer会重复生成time事件,而DispatcherTimer是一个集成到了Dispatcher队列中的时钟,这可以使它被按照指定的时间间隔以指定的priority定期执行。

对于一个Timer时钟事件,系统并不能保证在时间间隔到达后被立即执行,但是能够确保在时间间隔到达之前不被执行。这是因为DispatcherTimer像其他操作一样被放置在了Dispatcher队列中。何时执行DispatcherTimer事件依赖于队列中的其他任务以及他们的优先级.

如果一个WPF应用程序使用了Timer时钟,那么它的事件必须在一个单独的时钟线程中运行,而不是在UI线程中,这对于WPF应用程序毫无用处——你没法在UI线程之外直接访问UI元素,而只能通过Invoke或者BeginInvoke将操作发送给Dispatcher 对象,委托Dispatcher去执行UI操作。

看到这里,你大概知道了为什么我们在WPF中应该用DispatcherTimer而不是Timer了:DispatcherTimer与Dispatcher运行于同一个线程中——UI线程,而且具有相同的DispatcherPriority优先级。

所以,在WPF/Silverlight应用中,正确的做法如下所示:

[code lang=”c#”]

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(100);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();

[/code]

C# Tostring() 格式大全 [转]

C

 

货币

 

2.5.ToString(“C”)

 

¥2.50

 

D

 

十进制数

 

25.ToString(“D5”)

 

00025

 

E

 

科学型

 

25000.ToString(“E”)

 

2.500000E+005

 

F

 

固定点

 

25.ToString(“F2”)

 

25.00

 

G

 

常规

 

2.5.ToString(“G”)

 

2.5

 

N

 

数字

 

2500000.ToString(“N”)

 

2,500,000.00

 

X

 

十六进制

 

255.ToString(“X”)

 

FF

 

formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)

必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。

常用格式举例:

(1) int i=12345;

this.textBox1.Text=i.ToString();

//结果 12345(this指当前对象,或叫当前类的实例)

this.textBox2.Text=i.ToString(“d8”);

//结果 00012345

(2) int i=123;

double j=123.45;

string s1=string.Format(“the value is {0,7:d}”,i);

string s2=string.Format(“the value is {0,7:f3}”,j);

this.textBox1.Text=s1 ;

//结果 the value is 123

this.textBox2.Text=s2;

//结果 the value is 123.450

(3)double i=12345.6789;

this.textBox1.Text=i.ToString(“f2”); //结果 12345.68

this.textBox2.Text=i.ToString(“f6”);

//结果 12345.678900

(4)double i=12345.6789;

this.textBox1.Text=i.ToString(“n”); //结果 12,345.68

this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789

(5)double i=0.126;

string s=string.Format(“the value is {0:p}”,i);

this.textBox1.Text=i.ToString(“p”); //结果 12.6%

this.textBox2.Text=s; //结果 the value is 12.6%

(6) DateTime dt =new DateTime(2003,5,25);

this.textBox1.Text=dt.ToString(“yy.M.d”);

//结果 03.5.25

this.textBox2.Text=dt.ToString(“yyyy年M月”);

//结果 2003年5月

Convert.ToDateTime(“2005/12/22 22:22:22”).ToString(“yyyy/MM/dd HH:mm:ss”)
“2005/12/22 22:22:22”

(7) int i=123;

double j=123.45;

string s=string.Format(“i:{0,-7},j:{1,7}”,i,j);

//-7表示左对齐,占7位

this.textBox1.Text=s ;

//结果i:123 ,j: 123.45

 DateTime.ToString()用法详解

我们经常会遇到对时间进行转换,达到不同的显示效果,默认格式为:2006-6-6 14:33:34
如果要换成成200606,06-2006,2006-6-6或更多的格式该怎么办呢?
这里将要用到:DateTime.ToString的方法(String, IFormatProvider)
示例:
using System;
using System.Globalization;
String format=”D”;
DateTime date=DataTime.Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));
结果输出
Thursday, June 16, 2006

在这里列出了参数format格式详细用法
=======================
格式字符 关联属性/说明
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y、Y YearMonthPattern

下表列出了可被合并以构造自定义模式的模式
========================================
这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。

格式模式 说明 :
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd 周中某天的完整名称,在 DayNames 中定义。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM 月份的完整名称,在 MonthNames 中定义。
y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy 包括纪元的四位数的年份。
gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
f 秒的小数精度为一位。其余数字被截断。
ff 秒的小数精度为两位。其余数字被截断。
fff 秒的小数精度为三位。其余数字被截断。
ffff 秒的小数精度为四位。其余数字被截断。
fffff 秒的小数精度为五位。其余数字被截断。
ffffff 秒的小数精度为六位。其余数字被截断。
fffffff 秒的小数精度为七位。其余数字被截断。
t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
: 在 TimeSeparator 中定义的默认时间分隔符。
/ 在 DateSeparator 中定义的默认日期分隔符。
% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
\ c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。

只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,

DateTime.ToString( “d”) 返回 DateTime 值;“d”是标准短日期模式。
DateTime.ToString( “%d”) 返回月中的某天;“%d”是自定义模式。
DateTime.ToString( “d “) 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

比较方便的是,上面的参数可以随意组合,并且不会出错,多试试,肯定会找到你要的时间格式
如要得到2005年06月 这样格式的时间
可以这样写:
date.ToString(“yyyy年MM月”, DateTimeFormatInfo.InvariantInfo)
如此类推.

下面列出一些Asp.net中具体的日期格式化用法:
============================================
1.绑定时格式化日期方法:

2.数据控件如DataGrid/DataList等的件格式化日期方法:
e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString();

3.用String类转换日期显示格式:
String.Format( “yyyy-MM-dd “,yourDateTime);

4.用Convert方法转换日期显示格式:
Convert.ToDateTime(“2005-8-23”).ToString

(“yyMMdd”,System.Globalization.DateTimeFormatInfo.InvariantInfo); //支持繁体数据库

5.直接用ToString方法转换日期显示格式:
DateTime.Now.ToString(“yyyyMMddhhmmss”);
DateTime.Now.ToString(“yyyy/MM/dd hh:mm:ss”)

6.只显示年月
DataBinder.Eval(Container.DataItem,”starttime”,”{0:yyyy-M}”)

7.显示时间所有部分,包括:年月日时分秒
DataFormatString='{0:yyyy-MM-dd HH24:mm:ss}’>

用DateTime.ToString(string format)输出不同格式的日期

DateTime.ToString()函数有四个重载。一般用得多的就是不带参数的那个了。殊不知,DateTime.ToString(string format)功能更强大,能输出不同格式的日期。以下把一些情况罗列出来,供大家参考。有些在MSDN上有的就没有列出来了。

1.         y代表年份,注意是小写的y,大写的Y并不代表年份。

2.         M表示月份。

3.         d表示日期,注意D并不代表什么。

4.         h或H表示小时,h用的是12小时制,H用的是24小时制。

5.         m表示分钟。

6.         s表示秒。注意S并不代表什么。

格式

输出

示例

y 7 string yy = DateTime.Now.ToString(“y-MM”)

yy=”7-05″

yy 07 string yy = DateTime.Now.ToString(“yy-MM”)

yy=”07-05″

yyy或更多的y 1984 string yy = DateTime.Now.ToString(“yyyy”);

yy=”2007″

M 5. string mon = DateTime.Parse(“1984-05-09”)ToString(“yyyy-M”)

mon = “1984-5”

MM 05. string mon = DateTime.Parse(“1984-05-09”)ToString(“MM”)

mon = “05”

MMM 如果是中文版的操作系统,则会输出:五月.

如果是英文操作系统,则输入月份前三个字母的简写:May

string mon = DateTime.Parse(“2006-07-01”).ToString(“MMM”)

英文版操作系统:Jul

中文版操作系统:七月

MMMM或更多的M 如果是中文版的操作系统,则会输出:五月.

如果是英文操作系统,则输入月份的全写

string mon = DateTime.Parse(“2006-07-01”).ToString(“MMM”)

英文版操作系统:July

中文版操作系统:七月

日期或星期
d 9 string dd= DateTime.Parse(“1984-05-09”)ToString(“d”)

dd= “9”

 

dd 09 string dd= DateTime.Parse(“1984-05-09”)ToString(“dd”)

dd= “09”

ddd 如果是中文版的操作系统,则会输出星期,如星期三。.

如果是英文操作系统,则输出星期的简写:如

Wed

string dd = DateTime.Parse(“2006-07-01”).ToString(“ddd”)

英文版操作系统:Wed

中文版操作系统:星期三

dddd或更多的d 如果是中文版的操作系统,则会输出星期,如星期三。.

如果是英文操作系统,则输出星期:如

Wednesday

string dd = DateTime.Parse(“2006-07-01”).ToString(“dddd”)

英文版操作系统:Wednesday

中文版操作系统:星期三

小时
h 小时范围:1-12 string hh = DateTime.Now.ToString(“h”);

hh = 8

hh或更多的h 小时范围:1-12 string hh = DateTime.Now.ToString(“hh”);

hh = 08

H 小时范围:0-23 string hh = DateTime.Now.ToString(“yyyy-H”);

hh = 2006-8

HH或更多的H 小时范围:0-23 string hh = DateTime.Now.ToString(“yyyy-HH”);

hh = 2006-08

string hh = DateTime.Pare(“2006-7-4 18:00:00”).ToString(“yyyy-HH”);

hh = 2006-18

分钟
m 6 string mm = DateTime.Now.ToString(“yyyy-MM-dd-m”);

mm = “2006-07-01-6”;

mm或更多的m 06 string mm = DateTime.Now.ToString(“yyyy-MM-dd-mm”);

mm = “2006-07-01-06”;

s 6 string mm = DateTime.Now.ToString(“yyyy-MM-dd-s”);

mm = “2006-07-01-6”;

ss或更多的s 06 string mm = DateTime.Now.ToString(“yyyy-MM-dd-ss”);

mm = “2006-07-01-06”;

【原】实用的C#串口类CommPort,用于串口通信的源代码类

此CommPort串口类基于网上的mycomm修改,修复了一些bug、完善了事件处理机制,算是相当稳定实用了,不敢独享,贡献给大家想用。

使用注意事项:

1、几个属性的设置,用0、1、2、3等表示相应的选项;

2、port是数字,Open的时候自动串上“\\\\.\\COM”字符串;

3、加入了数据接收事件的触发,由于是从后台调用DataReceived回调函数,在接收和处理数据时可能不能访问UI元素(没试过,如果哪位碰到这个问题,自己加一下Invokee调用吧);

4、几个字段没有加正确性校验,别传错了;

5、读取数据时尽量用Read一次全部读出,然后做分解,而不是用ReadByte一次读一个字节,你想想,如果一块数据好几k,那么你用Read一次就读出来了,而ReadByte要读好几千次!性能马上就掉下来了;

6、低调的实现了startThread函数,你也会看到,如果要做的更好,线程启动时应该判断一下先前的线程是否退出;

7、你也应该看到,访问串口时没有加锁,安全的做法是,在读写串口时应该保证同时只有个读或者写,搞多线程的童鞋这里要注意一下;

8、跑的爽的时候想想我,出错的时候别骂我 :-)

—— HYH || khler

 

[code lang=”C”]

/*
* 作者:何元会
* E-mail:khler@163.com
* QQ:23381103
* 授权:GPL,同时必须保证本说明的完整性!
*
* 此串口类基于网上的一个类 mycomm 修改而来,网址:http://hi.baidu.com/p1220/blog/item/bf014e01d1d6da047aec2cb9.html
*
* 此处提到的mycomm类实际上是有bug的,就是DCB结构体的定义,对几个bit flag的位字段包装有问题,
* DCB中的这些flag每个只占一个bit,而包装中占了一个int,显然传递进去后标志位会错位,肯定会有问题。
* 我碰到的就是每次接收到的数据,大于127的数值都被折半了。修复DCB结构体对几个flag的定义就解决了。
* C#里面没有位字段定义,需要与枚举类型配合使用,此bug已修复;
*
* 另一个问题是,对于’USB转串口’的串口设备,在CreateFile时会打开失败的,报告找不到此文件。
* 通用做法是在前面加’\\\\.\\’,如COM1应该是"\\\\.\\COM1"
*
* 再一个改进:添加了数据接收事件探测,当有数据到达时调用事件处理函数。用法与自带的SerialPort类一样,
* 在定义对象时记得给他的DataReceived串接一个事件处理函数就行了。
*
* 加入功能:
* 1、包装了ClearCommError()函数,用于读取当前串口可读数据,BytesToRead,只读;
* 2、加入CommDataReceivedEventHandler委托,当有数据时向使用者发送消息;
* 3、加入数据监测线程,用于后台监视数据可读;
* 4、加入ReadByte函数,用于读取单个字节;
* 5、基本模拟了C#自带的SerialPort类,使用极其方便;
* 6、在读取数据后手动从BytesToRead中减去刚刚读取掉的字节数,避免多线程异步更新问题;
* 7、属性做了包装和说明;
* 8、修复DCB结构体定义bug;
* 9、修复无法打开’USB转串口’的串口设备的问题;
* 10、添加GetLastError()函数,打开设备失败时提取错误号;
*
*
*
*
* 接口:
* CommPort() ,构造函数
* DataReceived ,事件处理回调
* void Open() ,打开串口
* byte[] Read(int NumBytes) ,读取数据
* byte ReadByte() ,读取字节
* int Write(byte[] WriteBytes) ,写入数据
* void Close() ,关闭串口
*
*
* 使用范例:
*
private void OpenPort(int port)
{
try
{
comm = new CommPort();
comm.PortNum = port;
comm.BaudRate = 115200;
comm.ByteSize = 8;
comm.Parity = 0;
comm.StopBits = 0;
comm.ReadTimeout = 10;
comm.DataReceived += new CommDataReceivedEventHandler(comm_DataReceived);
comm.Open();
Console.WriteLine("COM" + comm.PortNum + " 打开成功");
}
catch (Exception ex)
{
Console.WriteLine("COM" + comm.PortNum + " 打开失败:" + ex.Message);
}
}

void comm_DataReceived(object sender)
{
if (comm.BytesToRead > 0)
{
byte[] buffer = comm.Read(comm.BytesToRead);
// 分解数据

}
}

*
*
**/

using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO.Ports;

namespace HYHComm
{
public delegate void CommDataReceivedEventHandler(object sender);

class CommPort
{
#region 构造函数
public CommPort()
{
}
#endregion// 构造函数

#region 属性
/// <summary>
/// 数据接收事件
/// </summary>
public event CommDataReceivedEventHandler DataReceived;

//comm port win32 file handle
private int hComm = -1;

/// <summary>
/// 打开标示
/// </summary>
public bool Opened;
/// <summary>
/// 串口号:1,2,3,4…
/// </summary>
public int PortNum;
/// <summary>
/// 波特率:1200,2400,4800,9600,115200
/// </summary>
public int BaudRate;
/// <summary>
/// 数据位 8 bits
/// </summary>
public byte ByteSize;
/// <summary>
/// 数据校验 0-4=no,odd,even,mark,space
/// </summary>
public byte Parity;
/// <summary>
/// 停止位 0,1,2 = 1, 1.5, 2
/// </summary>
public byte StopBits;
/// <summary>
/// 超时时间 //10
/// </summary>
public int ReadTimeout;
/// <summary>
/// 缓存中可读字节数
/// </summary>
public int BytesToRead
{
get { return _BytesToRead; }
}
private int _BytesToRead;

private enum ComStatFlags
{
fCtsHold = 0x1,
fDsrHold = 0x2,
fRlsdHold = 0x4,
fXoffHold = 0x8,
fXoffSent = 0x10,
fEof = 0x20,
fTxim = 0x40
}
[StructLayout(LayoutKind.Sequential)]
private struct COMSTAT
{
public ComStatFlags flags;
public uint cbInQue;
public uint cbOutQue;
};

private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;

private enum DCBFlags
{
fBinary = 0x01, // binary mode, no EOF check
fParity = 0x02, // enable parity checking
fOutxCtsFlow = 0x04, // CTS output flow control
fOutxDsrFlow = 0x08, // DSR output flow control
fDtrControl = 0x30, // DTR flow control type , 2bits
fDsrSensitivity = 0x40, // DSR sensitivity
fTXContinueOnXoff = 0x80, // XOFF continues Tx
fOutX = 0x100, // XON/XOFF out flow control
fInX = 0x200, // XON/XOFF in flow control
fErrorChar = 0x400, // enable error replacement
fNull = 0x800, // enable null stripping
fRtsControl = 0x3000, // RTS flow control , 2bits
fAbortOnError = 0x4000, // abort on error
fDummy2 = unchecked((int)0xffff8000), // reserved , 17bits
};
[StructLayout(LayoutKind.Sequential)]
private struct DCB
{
//taken from c struct in platform sdk
public int DCBlength; // sizeof(DCB)
public int BaudRate; // current baud rate
public DCBFlags fFlags; // DCBFlags
public ushort wReserved; // not currently used
public ushort XonLim; // transmit XON threshold
public ushort XoffLim; // transmit XOFF threshold
public byte ByteSize; // number of bits/byte, 4-8
public byte Parity; // 0-4=no,odd,even,mark,space
public byte StopBits; // 0,1,2 = 1, 1.5, 2
public char XonChar; // Tx and Rx XON character
public char XoffChar; // Tx and Rx XOFF character
public char ErrorChar; // error replacement character
public char EofChar; // end of input character
public char EvtChar; // received event character
public ushort wReserved1; // reserved; do not use
}
[StructLayout(LayoutKind.Sequential)]
private struct COMMTIMEOUTS
{
public int ReadIntervalTimeout;
public int ReadTotalTimeoutMultiplier;
public int ReadTotalTimeoutConstant;
public int WriteTotalTimeoutMultiplier;
public int WriteTotalTimeoutConstant;
}
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
}
#endregion// 属性

#region 方法
[DllImport("kernel32.dll")]
private static extern bool ClearCommError(int hFile, ref int lpdwErrorFlag, ref COMSTAT lpCommStat);
[DllImport("kernel32.dll")]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
[DllImport("kernel32.dll")]
private static extern bool GetCommState(int hFile, ref DCB lpDCB);
[DllImport("kernel32.dll")]
private static extern bool BuildCommDCB(string lpDef, ref DCB lpDCB);
[DllImport("kernel32.dll")]
private static extern bool SetCommState(int hFile, ref DCB lpDCB);
[DllImport("kernel32.dll")]
private static extern bool GetCommTimeouts(int hFile, ref COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
private static extern bool SetCommTimeouts(int hFile, ref COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
private static extern bool ReadFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead, ref OVERLAPPED lpOverlapped);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped);
[DllImport("kernel32.dll")]
private static extern UInt32 GetLastError();
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject);
private void startThread()
{
// 匿名方法
ParameterizedThreadStart threadRun = delegate(object self)
{
//COMM comm = (COMM)self;
int dwErrorFlags = 0;
COMSTAT ComStat = new COMSTAT();

while (Opened)
{
// 打破无限循环,降低CPU使用率
Thread.Sleep(0);
if (!ClearCommError(hComm, ref dwErrorFlags, ref ComStat))
{
continue;
}

if (dwErrorFlags != 0)
{
//getMsg("上一次对串口的操作存在错误");
continue;
}

_BytesToRead = (int)ComStat.cbInQue;

if (_BytesToRead > 0)
{
if (DataReceived != null)
{
DataReceived(this);
}
}
}
};
Thread thrHasData = new Thread(threadRun);
thrHasData.Start(this);
}

public void Open()
{
DCB dcbCommPort = new DCB();
COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();

// OPEN THE COMM PORT.
hComm = CreateFile("\\\\.\\COM" + PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

// IF THE PORT CANNOT BE OPENED, BAIL OUT.
if (hComm == INVALID_HANDLE_VALUE)
{
UInt32 dwErr = GetLastError();
throw (new ApplicationException("Comm Port Can Not Be Opened, Error Code:" + dwErr));
}

// SET THE COMM TIMEOUTS.
GetCommTimeouts(hComm, ref ctoCommPort);
ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
ctoCommPort.ReadTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutMultiplier = 0;
ctoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, ref ctoCommPort);

// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.

dcbCommPort.DCBlength = Marshal.SizeOf(dcbCommPort);
GetCommState(hComm, ref dcbCommPort);
dcbCommPort.BaudRate = BaudRate;
dcbCommPort.Parity = Parity;
dcbCommPort.ByteSize = ByteSize;
dcbCommPort.StopBits = StopBits;
SetCommState(hComm, ref dcbCommPort);

Opened = true;

startThread();
}

public byte[] Read(int NumBytes)
{
byte[] BufBytes = null;
byte[] OutBytes = null;
BufBytes = new byte[NumBytes];
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
if (ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort))
{
_BytesToRead -= BytesRead;
OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, OutBytes, BytesRead);
}
}
else
{
throw (new ApplicationException("Comm Port Not Open"));
}
return OutBytes;
}

public byte ReadByte()
{
byte[] BufBytes = new byte[1];
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
int BytesRead = 0;
if (ReadFile(hComm, BufBytes, 1, ref BytesRead, ref ovlCommPort))
{
_BytesToRead -= 1;
}
}
else
{
throw (new ApplicationException("Comm Port Not Open"));
}
return BufBytes[0];
}

public int Write(byte[] WriteBytes)
{
int BytesWritten = 0;
if (hComm != INVALID_HANDLE_VALUE)
{
OVERLAPPED ovlCommPort = new OVERLAPPED();
WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort);
}
else
{
throw (new ApplicationException("Comm Port Not Open"));
}
return BytesWritten;
}

public void Close()
{
// 置为false,监测线程将会退出
Opened = false;
if (hComm != INVALID_HANDLE_VALUE)
{
CloseHandle(hComm);
}
}
#endregion// 方法
}
}

[/code]

Thrift简介

Thrift是一个跨语言服务部署框架,最初由Facebook于2007年开发,后于2008年进入Apache孵化器(Apache Incubator)。

类似于SOAP,COM 和CORBA,Thrift通过定义一个中间定义语言和Thrift代码生成工具,生成指定语言的代码。目前,Thrift支持C++,Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk和OCaml的代码生成。

简单分析其机理,Thrift就是实现C/S模式,通过代码生成工具将接口定义文件生成服务器端和客户端代码(可以为不同语言),从而实现服务端和客户端跨语言的支持。

Thrift可以分为传输层和协议层:

1、传输层定义了数据的传输方式,可以为TCP/IP传输,内存共享或者文件共享等形式;
2、协议层定义了数据的传输格式,可以为二进制流或者XML等形式。

简单例子:

中间语言定义:

1 struct UserProfile {
2 1: i32 uid,
3 2: string name,
4 3: string blurb
5 }
6 service UserStorage {
7 void store(1: UserProfile user),
8 UserProfile retrieve(1: i32 uid)
9 }

Python客户端代码:

01 # Make an object
02 up = UserProfile(uid=1,
03                  name="Mark Slee",
04                  blurb="I'll find something to put here.")
05   
06 # Talk to a server via TCP sockets, using a binary protocol
07 transport = TSocket.TSocket("localhost", 9090)
08 transport.open()
09 protocol = TBinaryProtocol.TBinaryProtocol(transport)
10   
11 # Use the service we already defined
12 service = UserStorage.Client(protocol)
13 service.store(up)
14   
15 # Retrieve something as well
16 up2 = service.retrieve(2)

服务器端代码:

01 class UserStorageHandler : virtual public UserStorageIf {
02  public:
03   UserStorageHandler() {
04     // Your initialization goes here
05   }
06   
07   void store(const UserProfile& user) {
08     // Your implementation goes here
09     printf("store\n");
10   }
11   
12   void retrieve(UserProfile& _return, const int32_t uid) {
13     // Your implementation goes here
14     printf("retrieve\n");
15   }
16 };
17   
18 int main(int argc, char **argv) {
19   int port = 9090;
20   shared_ptr<UserStorageHandler> handler(new UserStorageHandler());
21   shared_ptr<TProcessor> processor(new UserStorageProcessor(handler));
22   shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
23   shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
24   shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
25   TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
26   server.serve();
27   return 0;
28 }

参考资料

1. http://incubator.apache.org/thrift

本文来自 http://www.imneio.com/2009/10/thrift-intro/

转载请保留原作者链接,谢谢!!!

RPC 数据交换格式

RPC简称远程调用,说白了就是不同机器之间的交流通信,不同平台之间的交流通信,牵涉到机器间的交流通信,就要做到跟平台无关,不论是linux,windows也好都可以进行相互间的交流,跟语言无关,不论是C,C++,java等,都能够近可能多的表达出够准确的意思,也就是说,数据交换的格式定义在分布式数据通讯当中很重要,传统的xml,json都能满足上述的功能需求,而对于一个大型的分布式应用,定义一种高效,更少的传输数据量,更快的解释和生成速度满足系统间的调用需求,对资源的节省十分可观,由此各个公司纷纷推出各自内部进行数据通讯的rpc数据格式。
目前使用最多的莫过于xml和json,而且它们相应的支持的语言和平台更多是一种通用的标准,像soap,rest等都采用了xml进行数据通讯,而json常用作WEB程序客户端跟服务器端进行数据通讯的一种交换格式。目前很火热的莫过于Google Protocol Buffer( 简称 Protobuf) ,Protobuf是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件。他们用于 RPC 系统和持续数据存储系统。Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API(摘抄自wikipedia)。facebook相应的也推出自己的一个叫thrift的rpc数据交换格式,thrift属于facebook.com技术核心框架之一,使用不同开发语言开发的系统可以通过该框架实现彼此间的通讯,开发者只需编辑一份 thrift脚本,即可自动获得与其它开发语言的代码进行数据通讯,它的数据文件定义格式跟Protobuf很像。Avro是著名的分布式计算框架hadoop采用的一种分布式的数据通讯格式,相应的可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。BSON是一种类似于json的一种数据格式,目前著名的nosql软件mongodb采用该格式进行数据通讯。最近看到日本的nosql大会上介绍了一个叫kumofs的分布式存储软件,它当中采用一个叫MessagePack的数据格式进行数据交换,从作者的介绍来看,它支持的功能要比上述的格式要多,支持异步rpc,Parallel Pipelining,Thrift IDL,动态的数据类型,连接池,延迟返回,事件驱动I/O类型等,而且作者号称速度要比其它的数据格式小,解释和生成速度快。具体可参照文档

rpc数据交换格式多,相应的的选择的余力也就更大,到底那一种选择那一种,传统的xml,json也够用,新奇的也很有诱惑力,关键在于那一种适合你当前的需求。

本文转自:http://blog.netzhou.net/?p=101

转载请注明原作者及出处!

【转】典型开源3D引擎分类比较

 

典型开源3D引擎分类比较

 

常见的3D引擎有:Unreal,Quake,Lithtech,OGRE,Nebula,Irrlicht,Truevision3D…等,其中开源免费的有:orge,irrlicht,fly3d, NeoEngine, revolution3d, Nebula2Genesis3d等。OGRE 在免费、开源的引擎中评价最高,一个原因是作为一个图形渲染引擎,它支持的图形特性最多,所以渲染质量也不错;另一个原因是设计模式的清晰;另外,速度也不错。OGRE 将专注于向一个纯粹的、然而富于协作和扩展性的图形引擎发展,这得益于它的庞大的社群支持,使得很多事情可以通过外挂一些更专业的引擎来实现,物理引擎使用 ODE  Tokamak  NovodeX ,网络引擎使用 openTNL  RakNet  eNet ,声音引擎使用 FMod  OpenAL ,以及界面引擎使用 CEGUI 

 

 

Standard

OGRE

IrrLicht

Truevision3D

General

Type

Only rendering engine

 

Game engine

Include TV3DEngine,TV3DMedia,TV3DNet……

Languages

Written in pure C++, totally object orientated.

Written in pure C++, totally object orientated.

Written in C++ and VB6 with DirectX8.1

License

LGPL

zlib/libpng License(totally free)

·   Free TV3DSDK

·   Registered Developer License

·   group or site licenses

Develop platforms

·   C++

·   .NET

·   C++

·   .NET language binding

VB/VC++/DELPHI/

C#/vb.net

Platforms

Windows

ü         Support

ü         Support

ü         Support

Linux

 

ü         Support

ü         Support

 

MacOS

ü         Support

ü         Support

 

3D API

Direct3D

ü         Support

ü         Support

ü         Support

OpenGL

ü         Support

ü         Support

 

Other

 

·   The Irrlicht Engine software renderer.

·   The Apfelbaum Software Renderer

 

Architecture

Extensibility

·   Extensible example framework

·   Flexible plugin architecture allows engine to be extended without recompilation

·   Use some other professional engines to achieve other features

 

 

Scripting

 

 

 

Materials & Textures

Materials

·   Powerful material declaration language

·   Multiple material

·   Material LOD

·   Multi-texture and multi-pass blending

·   Projective texturing

·   Textures can be provided and updated in real-time by plugins

·   Provide common built in materials based on fixed function pipeline or programmable pipeline

·   add new materials to Irrlicht at runtime, without the need of modifying/recompiling the engine

 

Textures and Mapping

自动产生MipMap

 

自动调整纹理大小以满足硬件需求。

 

支持可程序控制的纹理坐标生成和转换。

·   Bump mapping

·   Parallax mapping

·   Light maps

·   Sphere mapping

DOT3 Bump mapping

 

没有提供多层纹理和混合模式

Shaders

·   Supports vertex and fragment programs, both written in assembler, and in Cg, DirectX9 HLSL, or GLSL

·   Support many commonly bound constant parameters like worldview matrices, light state information, object space eye position etc

·   Pixel and Vertex Shaders 1.1 to 3.0

·   ARB Fragment and Vertex Programs

·   HLSL

·   GLSL

·   Hardware Vertex Shaders

·   HLSL

Scene Management

General

·   Highly customizable, flexible scene management, not tied to any single scene type.

·   Hierarchical scene graph; nodes allow objects to be attached to each other and follow each others movements, articulated structures etc

·   Scene querying

·   Using a hierarchical scene graph, can mix indoor and outdoor scene seamless together

·   Extensible hierarchical scene management

 

 

BSP Support

BSP 场景的天空穹、粒子特效、高级材质属性,乃至 BSP 的空间分割规则都会被正确识别、自动实现

·    BSP 的特性支持比较贫乏

 BSP 的读取存在 BUG

Other

 

 

 

Special effects

Particle System

 

·   easily extensible emitters, affectors and renderers

·   can be defined in text scripts for adjusting in real-time

·   particle pooling

·   Customizeable Particle systems for snow, smoke, fire, …

Atmospheric effects like fog, cloud, sky (box and sphere), stars.

Light & Shadow

 

当前支持三种阴影:调整纹理阴影,调整模版阴影,附加模板阴影。不支持软阴影

Multiple shadow rendering techniques,

·   Dynamic lights

·   Dynamic shadows using the stencil buffer

·   Optimized dynamic stencil shadows

·   DirectX Hardware Lighting

Other

·   Support for skyboxes, skyplanes and skydomes

·   Billboarding for sprite graphics

·   Transparent objects automatically managed

 

·   Realistic water surfaces

·   Billboards

·   Transparent objects

·   Skyboxes

·   Fog

·   Texture animation

Billboard

Character Animation

 

·   Skeletal animation

·   blending of multiple animations

·   variable bone weight skinning

·   hardware-accelerated skinning

 

·   Morph target animation:

·   Skeletal animation

·   Skeleton based, Key frame based, morph based animations

·   Animations that can be enhanced by bone attaching or custom bone rotations.

Supported Formats

textures file formats

·   PNG, JPEG, TGA, BMP or DDS

·   1D textures, volumetric textures, cubemaps and compressed textures (DXT/S3TC)

·   PSD,JPG,PNG,TGA,BMP,PCX 

 

 

mesh file formats

Milkshape3D, 3D Studio Max, Maya, Blender and Wings3D

·   3DS,OBJ,CSM,DAE,DMF,OCT,X,MS3D,MY3D,LMTS,BSP,MD2

3DS, X, MDL, MD2, MD3

Document

 

Great number of users and well documented.

well documented API with lots of examples and tutorials.

 

Other

GUI

 

 

 

2D Drawing

 

 

 

XML

ü         Support

XML Converter: convert efficient runtime binary formats to/from XML for interchange or editing

ü        Support

快速的XML分析器

 

Web Site

 

http://www.ogre3d.org

http://irrlicht.sourceforge.net

http://www.truevision3d.com

 


 本文来自:http://www.cnblogs.com/cyberchen-net/archive/2007/03/04/663597.html

 

VB.NET and C# 语法比较手册

 

VB.NET and C# Comparison
This is a quick reference guide to highlight some key syntactical differences between VB.NETand C#. Hope you find this useful!
Thank you to Tom Shelton, Fergus Cooney, and others for your input.

VB.NET

C#

Comments
‘ Single line only
Rem Single line only

// Single line
/* Multiple
    line  */
/// XML comments on single line
/** XML comments on multiple lines */

Data Types

Value Types
Boolean
Byte
Char   (example: “A”c)
Short, Integer, Long
Single, Double
Decimal
Date

Reference Types
Object
String

Dim x As Integer
Console.WriteLine(x.GetType())     ‘ Prints System.Int32
Console.WriteLine(TypeName(x))  ‘ Prints Integer

‘ Type conversion
Dim numDecimal As Single = 3.5
Dim numInt As Integer
numInt = CType(numDecimal, Integer)   ‘ set to 4 (Banker’s rounding)
numInt = CInt(numDecimal)  ‘ same result as CType
numInt = Int(numDecimal)    ‘ set to 3 (Int function truncates the decimal)

Value Types
bool
byte, sbyte
char   (example: ‘A’)
short, ushort, int, uint, long, ulong
float, double
decimal
DateTime   (not a built-in C# type)

Reference Types
object
string

int x;
Console.WriteLine(x.GetType());    // Prints System.Int32
Console.WriteLine(typeof(int));      // Prints System.Int32


// Type conversion

double numDecimal = 3.5;
int numInt = (int) numDecimal;   // set to 3  (truncates decimal)

Constants
Const MAX_STUDENTS As Integer = 25 const int MAX_STUDENTS = 25;
Enumerations
Enum Action
  Start 
  [Stop]   ‘ Stop is a reserved word
  Rewind
  Forward
End Enum

Enum Status
  Flunk = 50
  Pass = 70
  Excel = 90
End Enum

Dim a As Action = Action.Stop
If a <> Action.Start Then Console.WriteLine(a)     ‘ Prints 1

Console.WriteLine(Status.Pass)     ‘ Prints 70

Dim s As Type = GetType(Status)
Console.WriteLine([Enum].GetName(s, Status.Pass))    ‘ Prints Pass

enum Action {Start, Stop, Rewind, Forward};
enum Status {Flunk = 50, Pass = 70, Excel = 90};

Action a = Action.Stop;
if (a != Action.Start)
  Console.WriteLine(a + ” is ” + (int) a);    // Prints “Stop is 1”

Console.WriteLine(Status.Pass);    // Prints Pass

Operators

Comparison
=  <  >  <=  >=  <>

Arithmetic
+  –  *  /
Mod
(integer division)
(raise to a power)

Assignment
=  +=  -=  *=  /=  \=  ^=  <<=  >>=  &=

Bitwise
And  AndAlso  Or  OrElse  Not  <<  >>

Logical
And  AndAlso  Or  OrElse  Not

Note: AndAlso and OrElse are for short-circuiting logical evaluations

String Concatenation
&

Comparison
==  <  >  <=  >=  !=

Arithmetic
+  –  *  /
(mod)
(integer division if both operands are ints)
Math.Pow(x, y)

Assignment
=  +=  -=  *=  /=   %=  &=  |=  ^=  <<=  >>=  ++  —

Bitwise
&  |  ^   ~  <<  >>

Logical
&&  ||   !

Note: && and || perform short-circuit logical evaluations

String Concatenation
+

Choices

greeting = IIf(age < 20, “What’s up?”, “Hello”)

‘ One line doesn’t require “End If”, no “Else”
If language = “VB.NET” Then langType = “verbose”

‘ Use : to put two commands on same line
If x <> 100 Then x *= 5 : y *= 2  

‘ or to break up any long single command use _
If whenYouHaveAReally < longLine And itNeedsToBeBrokenInto2 > Lines Then _
  UseTheUnderscore(charToBreakItUp)

If x > 5 Then
  x *= y
ElseIf x = 5 Then
  x += y
ElseIf x < 10 Then
  x -= y
Else
  x /= y
End If

Select Case color   ‘ Must be a primitive data type
  Case “pink”, “red”
    r += 1
  Case “blue”
    b += 1
  Case “green”
    g += 1
  Case Else
    other += 1
End Select

greeting = age < 20 ? “What’s up?” : “Hello”;

if (x != 100) {    // Multiple statements must be enclosed in {}
  x *= 5;
  y *= 2;
}

No need for _ or : since ; is used to terminate each statement.


if
(x > 5)
  x *= y;
else if (x == 5)
  x += y;
else if (x < 10)
  x -= y;
else
  x /= y;

switch (color) {                          // Must be integer or string
  case “pink”:
  case “red”:    r++;    break;        // break is mandatory; no fall-through
  case “blue”:   b++;   break;
  case “green”: g++;   break;
  default:    other++;   break;       // break necessary on default
}

Loops
Pre-test Loops:
While c < 10
  c += 1
End While

Do Until c = 10 
  c += 1
Loop

Do While c < 10
  c += 1
Loop

For c = 2 To 10 Step 2
  Console.WriteLine(c)
Next


Post-test Loops:
Do 
  c += 1
Loop While c < 10
Do 
  c += 1
Loop Until c = 10

‘  Array or collection looping
Dim names As String() = {“Fred”, “Sue”, “Barney”}
For Each s As String In names
  Console.WriteLine(s)
Next

Pre-test Loops:  

// no “until” keyword
while (i < 10)
  i++;

for (i = 2; i < = 10; i += 2)
  Console.WriteLine(i);

Post-test Loop:

do
  i++;
while (i < 10);

// Array or collection looping

string[] names = {“Fred”, “Sue”, “Barney”};
foreach (string s in names)
  Console.WriteLine(s);

Arrays

Dim nums() As Integer = {1, 2, 3} 
For i As Integer = 0 To nums.Length – 1
  Console.WriteLine(nums(i))
Next

‘ 4 is the index of the last element, so it holds 5 elements
Dim names(4) As String
names(0) = “David”
names(5) = “Bobby”  ‘ Throws System.IndexOutOfRangeException

‘ Resize the array, keeping the existing values (Preserve is optional)
ReDim Preserve names(6)

Dim twoD(rows-1, cols-1) As Single
twoD(2, 0) = 4.5

Dim jagged()() As Integer = { _
  New Integer(4) {}, New Integer(1) {}, New Integer(2) {} }
jagged(0)(4) = 5

int[] nums = {1, 2, 3};
for (int i = 0; i < nums.Length; i++)
  Console.WriteLine(nums[i]);

// 5 is the size of the array
string[] names = new string[5];
names[0] = “David”;
names[5] = “Bobby”;   // Throws System.IndexOutOfRangeException

// C# doesn’t can’t dynamically resize an array.  Just copy into new array.
string[] names2 = new string[7];
Array.Copy(names, names2, names.Length);   // or names.CopyTo(names2, 0); 

float[,] twoD = new float[rows, cols];
twoD[2,0] = 4.5f; 

int[][] jagged = new int[3][] {
  new int[5], new int[2], new int[3] };
jagged[0][4] = 5;

Functions

‘ Pass by value (in, default), reference (in/out), and reference (out) 
Sub TestFunc(ByVal x As Integer, ByRef y As Integer, ByRef z As Integer)
  x += 1
  y += 1
  z = 5
End Sub

Dim a = 1, b = 1, c As Integer   ‘ c set to zero by default 
TestFunc(a, b, c)
Console.WriteLine(“{0} {1} {2}”, a, b, c)   ‘ 1 2 5

‘ Accept variable number of arguments
Function Sum(ByVal ParamArray nums As Integer()) As Integer
  Sum = 0 
  For Each i As Integer In nums
    Sum += i
  Next
End Function   ‘ Or use Return statement like C#

Dim total As Integer = Sum(4, 3, 2, 1)   ‘ returns 10

‘ Optional parameters must be listed last and must have a default value
Sub SayHello(ByVal name As String, Optional ByVal prefix As String = “”)
  Console.WriteLine(“Greetings, ” & prefix & ” ” & name)
End Sub

SayHello(“Strangelove”, “Dr.”)
SayHello(“Madonna”)

// Pass by value (in, default), reference (in/out), and reference (out)
void TestFunc(int x, ref int y, out int z) {
  x++;  
  y++;
  z = 5;
}

int a = 1, b = 1, c;  // c doesn’t need initializing
TestFunc(a, ref b, out c);
Console.WriteLine(“{0} {1} {2}”, a, b, c);  // 1 2 5

// Accept variable number of arguments
int Sum(params int[] nums) {
  int sum = 0;
  foreach (int i in nums)
    sum += i;
  return sum;
}

int total = Sum(4, 3, 2, 1);   // returns 10

/* C# doesn’t support optional arguments/parameters.  Just create two different versions of the same function. */ 
void SayHello(string name, string prefix) {
  Console.WriteLine(“Greetings, ” + prefix + ” ” + name);

void SayHello(string name) {
  SayHello(name, “”);
}

Exception Handling

‘ Deprecated unstructured error handling
On Error GoTo MyErrorHandler

MyErrorHandler: Console.WriteLine(Err.Description)

Dim ex As New Exception(“Something is really wrong.”)
Throw  ex 

Try 
  y = 0
  x = 10 / y
Catch ex As Exception When y = 0 ‘ Argument and When is optional
  Console.WriteLine(ex.Message)
Finally
  Beep()
End Try

Exception up = new Exception(“Something is really wrong.”);
throw up;  // ha ha

try
  y = 0;
  x = 10 / y;
}
catch (Exception ex) {   // Argument is optional, no “When” keyword 
  Console.WriteLine(ex.Message);
}
finally {
  // Must use unmanaged MessageBeep API function to beep
}

Namespaces

Namespace Harding.Compsci.Graphics 
  …
End Namespace

‘ or

Namespace Harding
  Namespace Compsci
    Namespace Graphics 
      …
    End Namespace
  End Namespace
End Namespace

Import Harding.Compsci.Graphics

namespace Harding.Compsci.Graphics {
  …
}

// or

namespace Harding {
  namespace Compsci {
    namespace Graphics {
      …
    }
  }
}

using Harding.Compsci.Graphics;

Classes / Interfaces

Accessibility keywords
Public
Private
Friend                   
Protected
Protected Friend
Shared

‘ Inheritance
Class FootballGame
  Inherits Competition
  …
End Class 

‘ Interface definition
Interface IAlarmClock 
  …
End Interface

// Extending an interface 
Interface IAlarmClock
  Inherits IClock
  …
End Interface

// Interface implementation
Class WristWatch 
  Implements IAlarmClock, ITimer 
   …
End Class 

Accessibility keywords
public
private
internal
protected
protected internal
static

// Inheritance
class FootballGame : Competition {
  …
}


// Interface definition

interface IAlarmClock {
  …
}

// Extending an interface 
interface IAlarmClock : IClock {
  …
}


// Interface implementation

class WristWatch : IAlarmClock, ITimer {
   …
}

Constructors / Destructors
Class SuperHero
  Private _powerLevel As Integer

  Public Sub New ()
    _powerLevel = 0
  End Sub

  Public Sub New (ByVal powerLevel As Integer)
    Me._powerLevel = powerLevel
  End Sub

  Protected Overrides Sub Finalize () 
   ‘ Desctructor code to free unmanaged resources
    MyBase.Finalize()
  End Sub
End Class

class SuperHero {
  private int _powerLevel;

  public SuperHero() {
     _powerLevel = 0;
  }

  public SuperHero(int powerLevel) {
    this._powerLevel= powerLevel; 
  }

  ~SuperHero() {
    // Destructor code to free unmanaged resources.
    // Implicitly creates a Finalize method

  }
}

Objects

Dim hero As SuperHero = New SuperHero
With hero
  .Name = “SpamMan”
  .PowerLevel = 3
End With

hero.Defend(“Laura Jones”)
hero.Rest()     ‘ Calling Shared method
‘ or
SuperHero.Rest()

Dim hero2 As SuperHero = hero  ‘ Both refer to same object
hero2.Name = “WormWoman”
Console.WriteLine(hero.Name)   ‘ Prints WormWoman

hero = Nothing    ‘ Free the object

If hero Is Nothing Then _
  hero = New SuperHero

Dim obj As Object = New SuperHero
If TypeOf obj Is SuperHero Then _
  Console.WriteLine(“Is a SuperHero object.”)

SuperHero hero = new SuperHero();

// No “With” construct
hero.Name = “SpamMan”;
hero.PowerLevel = 3;

hero.Defend(“Laura Jones”);
SuperHero.Rest();   // Calling static method

SuperHero hero2 = hero;   // Both refer to same object
hero2.Name = “WormWoman”;
Console.WriteLine(hero.Name);   // Prints WormWoman

hero = null ;   // Free the object

if (hero == null)
  hero = new SuperHero();

Object obj = new SuperHero(); 
if (obj is SuperHero)
  Console.WriteLine(“Is a SuperHero object.”);

Structs

Structure StudentRecord
  Public name As String
  Public gpa As Single

  Public Sub New(ByVal name As String, ByVal gpa As Single)
    Me.name = name
    Me.gpa = gpa
  End Sub
End Structure

Dim stu As StudentRecord = New StudentRecord(“Bob”, 3.5)
Dim stu2 As StudentRecord = stu  

stu2.name = “Sue”
Console.WriteLine(stu.name)    ‘ Prints Bob
Console.WriteLine(stu2.name)  ‘ Prints Sue

struct StudentRecord {
  public string name;
  public float gpa;

  public StudentRecord(string name, float gpa) {
    this.name = name;
    this.gpa = gpa;
  }
}

StudentRecord stu = new StudentRecord(“Bob”, 3.5f);
StudentRecord stu2 = stu;  

stu2.name = “Sue”;
Console.WriteLine(stu.name);    // Prints Bob
Console.WriteLine(stu2.name);   // Prints Sue

Properties

Private _size As Integer

Public Property Size() As Integer
  Get
    Return _size
  End Get
  Set (ByVal Value As Integer)
    If Value < 0 Then
      _size = 0
    Else
      _size = Value
    End If
  End Set
End Property

foo.Size += 1

private int _size;

public int Size {
  get {
    return _size;
  }
  set {
    if (value < 0)
      _size = 0;
    else
      _size = value;
  }
}

foo.Size++;

Delegates / Events

Delegate Sub MsgArrivedEventHandler(ByVal message As String)

Event MsgArrivedEvent As MsgArrivedEventHandler

‘ or to define an event which declares a delegate implicitly
Event MsgArrivedEvent(ByVal message As String)

AddHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback
‘ Won’t throw an exception if obj is Nothing
RaiseEvent MsgArrivedEvent(“Test message”)
RemoveHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback

Imports System.Windows.Forms

Dim WithEvents MyButton As Button   ‘ WithEvents can’t be used on local variable
MyButton = New Button

Private Sub MyButton_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles MyButton.Click
  MessageBox.Show(Me, “Button was clicked”, “Info”, _
    MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

delegate void MsgArrivedEventHandler(string message);

event MsgArrivedEventHandler MsgArrivedEvent;

// Delegates must be used with events in C#

MsgArrivedEvent += new MsgArrivedEventHandler(My_MsgArrivedEventCallback);
MsgArrivedEvent(“Test message”);    // Throws exception if obj is null
MsgArrivedEvent -= new MsgArrivedEventHandler(My_MsgArrivedEventCallback);

using System.Windows.Forms;

Button MyButton = new Button(); 
MyButton.Click += new System.EventHandler(MyButton_Click);

private void MyButton_Click(object sender, System.EventArgs e) {
  MessageBox.Show(this, “Button was clicked”, “Info”,
    MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Console I/O

Special character constants
vbCrLf, vbCr, vbLf, vbNewLine
vbNullString
vbTab
vbBack
vbFormFeed
vbVerticalTab
“”
Chr(65)  ‘ Returns ‘A’

Console.Write(“What’s your name? “)
Dim name As String = Console.ReadLine()
Console.Write(“How old are you? “)
Dim age As Integer = Val(Console.ReadLine())
Console.WriteLine(“{0} is {1} years old.”, name, age) 
‘ or
Console.WriteLine(name & ” is ” & age & ” years old.”)

Dim c As Integer
c = Console.Read()    ‘ Read single char
Console.WriteLine(c)   ‘ Prints 65 if user enters “A”

Escape sequences
\n, \r
\t
\\
\”

Convert.ToChar(65)  // Returns ‘A’ – equivalent to Chr(num) in VB
// or

(char) 65

Console.Write(“What’s your name? “);
string name = Console.ReadLine();
Console.Write(“How old are you? “);
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“{0} is {1} years old.”, name, age);
// or
Console.WriteLine(name + ” is ” + age + ” years old.”);

int c = Console.Read();  // Read single char
Console.WriteLine(c);    // Prints 65 if user enters “A”

File I/O

Imports System.IO

Dim writer As StreamWriter = File.CreateText(“c:\myfile.txt”)
writer.WriteLine(“Out to file.”)
writer.Close()

Dim reader As StreamReader = File.OpenText(“c:\myfile.txt”)
Dim line As String = reader.ReadLine()
While Not line Is Nothing
  Console.WriteLine(“line=” & line)
  line = reader.ReadLine()
End While
reader.Close()

Dim str As String = “Text data”
Dim num As Integer = 123
Dim binWriter As New BinaryWriter (File.OpenWrite(“c:\myfile.dat”)) 
binWriter.Write(str) 
binWriter.Write(num) 
binWriter.Close()

Dim binReader As New BinaryReader (File.OpenRead(“c:\myfile.dat”))
str = binReader.ReadString()
num = binReader.ReadInt32()
binReader.Close()

using System.IO;

StreamWriter writer = File.CreateText(“c:\\myfile.txt”);
writer.WriteLine(“Out to file.”);
writer.Close();

StreamReader reader = File.OpenText(“c:\\myfile.txt”);
string line = reader.ReadLine();
while (line != null) {
  Console.WriteLine(line);
  line = reader.ReadLine();
}
reader.Close();

string str = “Text data”;
int num = 123;
BinaryWriter binWriter = new BinaryWriter(File.OpenWrite(“c:\\myfile.dat”));
binWriter.Write(str);
binWriter.Write(num);
binWriter.Close();

BinaryReader binReader = new BinaryReader(File.OpenRead(“c:\\myfile.dat”));
str = binReader.ReadString();
num = binReader.ReadInt32();
binReader.Close();

Page last modified: 04/20/2006 16:39:28 01/14/2004 05:34:59
Please send any corrections or comments to fmccown@harding.edu.
Home

C#中使用多線程訪問Winform問題

 

我們在做winform應用的時候,大部分情況下都會碰到使用多線程控制界面上控件信息的問題。然而我們并不能用傳統方法來做這個問題,下面我將詳細的介紹。

首先來看傳統方法:

 

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(ThreadFuntion);
thread.IsBackground = true;
thread.Start();
}
private void ThreadFuntion()
{
while (true)
{
this.textBox1.Text = DateTime.Now.ToString();
Thread.Sleep(1000);
}
}
}

 

運行這段代碼,我們會看到系統拋出一個異常:Cross-thread operation not valid:Control ‘textBox1’ accessed from a thread other than the thread it was created on . 這是因為.net 2.0以后加強了安全機制,不允許在winform中直接跨線程訪問控件的屬性。那么怎么解決這個問題呢,下面提供幾種方案。

第一種方案,我們在Form1_Load()方法中加一句代碼:

 

private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
Thread thread = new Thread(ThreadFuntion);
thread.IsBackground = true;
thread.Start();
}

 

加入這句代碼以后發現程序可以正常運行了。這句代碼就是說在這個類中我們不檢查跨線程的調用是否合法(如果沒有加這句話運行也沒有異常,那么說明系統以及默認的采用了不檢查的方式)。然而,這種方法不可取。我們查看CheckForIllegalCrossThreadCalls 這個屬性的定義,就會發現它是一個static的,也就是說無論我們在項目的什么地方修改了這個值,他就會在全局起作用。而且像這種跨線程訪問是否存在異常,我們通常都會去檢查。如果項目中其他人修改了這個屬性,那么我們的方案就失敗了,我們要采取另外的方案。

下面來看第二種方案,就是使用delegate和invoke來從其他線程中控制控件信息。網上有很多人寫了這種控制方式,然而我看了很多這種帖子,表明上看來是沒有什么問題的,但是實際上并沒有解決這個問題,首先來看網絡上的那種不完善的方式:

 

public partial class Form1 : Form
{
private delegate void FlushClient();//代理
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(CrossThreadFlush);

thread.IsBackground=true;
thread.Start();
}

private void CrossThreadFlush()
{
//將代理綁定到方法
FlushClient fc = new FlushClient(ThreadFuntion);
this.BeginInvoke(fc);//調用代理
}
private void ThreadFuntion()
{
while (true)
{
this.textBox1.Text = DateTime.Now.ToString();
Thread.Sleep(1000);
}
}
}

 

使用這種方式我們可以看到跨線程訪問的異常沒有了。但是新問題出現了,界面沒有響應了。為什么會出現這個問題,我們只是讓新開的線程無限循環刷新,理論上應該不會對主線程產生影響的。其實不然,這種方式其實相當于把這個新開的線程“注入”到了主控制線程中,它取得了主線程的控制。只要這個線程不返回,那么主線程將永遠都無法響應。就算新開的線程中不使用無限循環,使可以返回了。這種方式的使用多線程也失去了它本來的意義。

現在來讓我們看看推薦的解決方案:

 

public partial class Form1 : Form
{
private delegate void FlushClient();//代理
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(CrossThreadFlush);
thread.IsBackground = true;
thread.Start();
}

private void CrossThreadFlush()
{
while (true)
{
//將sleep和無限循環放在等待異步的外面
Thread.Sleep(1000);
ThreadFunction();
}
}
private void ThreadFunction()
{
if (this.textBox1.InvokeRequired)//等待異步
{
FlushClient fc = new FlushClient(ThreadFunction);
this.Invoke(fc);//通過代理調用刷新方法
}
else
{
this.textBox1.Text = DateTime.Now.ToString();
}
}
}

 

運行上述代碼,我們可以看到問題已經被解決了,通過等待異步,我們就不會總是持有主線程的控制,這樣就可以在不發生跨線程調用異常的情況下完成多線程對winform多線程控件的控制了。

 

 發佈時間:3/18/2009

 http://www.jlorocks.com/content.aspx?conId=2395

 

在CruiseControl.net 中用 devenv.com 自动生成 *.msi 安装文件

    利用CCNet(CruiseControl.Net)做打包文件时,碰到了一个问题,就是MSBuild没法编译Setup工程(*.vdproj),改用devenv.com配置,即可正常生成:

    vs2010 rc1通过,正常生成了安装文件。


    
<project name=”projectname”>
      
<workingDirectory></workingDirectory>
      
<artifactDirectory></artifactDirectory>
      
<labeller type=”defaultlabeller”>
        
<incrementOnFailure>False</incrementOnFailure>
        
<initialBuildLabel>1588</initialBuildLabel>
        
<labelFormat>0</labelFormat>
        
<postfix />
        
<prefix />
      
</labeller>
      
<sourcecontrol type=”svn”>
        
<trunkUrl>svn://。。。</trunkUrl>
        
<workingDirectory></workingDirectory>
        
<autoGetSource>true</autoGetSource>
        
<deleteObstructions>true</deleteObstructions>
        
<cleanUp>true</cleanUp>
        
<timeout>600000</timeout>
        
<username></username>
        
<password></password>
      
</sourcecontrol>
      
<tasks>
        
<devenv>
          
<executable>D:\Microsoft Visual Studio 10.0\Common7\IDE\devenv.com</executable>
          
<solutionfile>D:\ccnetPrj\Culvert.sln</solutionfile>
          
<buildtype>Build</buildtype>
          
<configuration>Debug</configuration>
          
<buildTimeoutSeconds>60000</buildTimeoutSeconds>
        
</devenv>
      
</tasks>
      ……
      ……
      ……