2010-07-22

C#中用DllImport调用非托管代码

基本用法:调用系统的messagebox(位于user32.dll中)

using System;
using System.Runtime.InteropServices;
class Example
{    // Use DllImport to import the Win32 MessageBox function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

    static void Main()
    {
        // Call the MessageBox function using platform invoke.
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}
至少要制定要导入的dll名称,可选字段:

  • CharSet:制定字符集
  • EntryPoint:制定入口点

     入口点用于标识函数在 DLL 中的位置。 在托管对象中,目标函数的原名或序号入口点将  标识跨越交互操作边界的函数。 此外,您可以将入口点映射到一个不同的名称,这实际上是将函数重命名。

以下列出了重命名 DLL 函数的可能原因:
  1. 避免使用区分大小写的 API 函数名
  2. 符合现行的命名标准
  3. 提供采用不同数据类型的函数(通过声明同一 DLL 函数的多个版本)
  4. 简化对包含 ANSI 和 Unicode 版本的 API 的使用
下面的示例演示如何使用 EntryPoint 字段将代码中的 MessageBoxA 替换为 MsgBox。

using System.Runtime.InteropServices;

public class Win32 {
    [DllImport("user32.dll", EntryPoint="MessageBoxA")]
    public static extern int MsgBox(int hWnd, String text, String caption,
                                    uint type);
}

2010-07-19

捷通华声面试

时间:2010-7-19 下午
地点:中关村软件园10号楼(great road)
本来让我上周六下午去,有事情没去成,改成了今天下午,事先去他们网站上了解了一下,做语音识别,手写识别的。进去前台有两个mm,桌子上有祝xxx生日快乐的小牌子,比较人性化,右边边白板上贴着竞赛排名,测试的居多,右边摆放这很多奖牌,奖状,荣誉证书,有一个是“副主任奖”,当然这都是做完题在那儿等着的时候看到的。去了先做题,技术一份,非技术一份
技术题:多选,填空
实现二分查找

软件开发中,术语 架构,模式,过程,框架都是什么意思

聚簇索引和非聚簇索引

一张表ppp,找出id最大的一行

asp.net页面周期

httphandler 和 httpmodule

一段重写多态程序的输出结果
。。。
A a = new A();
B b = new B();
a.Fun2(b);
b.Fun1(a);

写出三种设计模式,说明其中一种的使用场景及实现的关键点

职业规划,最近关注的技术


非技术题
希望有什么样的同事和领导

你是否知道相对论?简单写一下你了解的

如果你家水龙头坏了,你怎么办

你和你的朋友开了一家饭店,职位有财务,大堂主管,厨房主管,保安,前台,采购,等职位,你想干什么?

让你在荒岛上待一年,带3本(种)书,2个CD,你希望带什么书和CD?
我写的是24史,算法,经济,CD是雅尼和许巍

估算常年每年流入长江多少吨水?
365*24*60*60  *  100*3  *0.2

假如你来公司上班,你认为什么样的绩效考核对员工有帮助

你有什么优势,能为公司做出什么贡献

背景调查,写出以前公司的人力姓名,联系方式
我开始填了一个,另一人的电话没写,人力面的时候让我回来找到在发给他们,我就在技术二面之前上msn找到,临走的时候让前台mm写上了

之后是技术面试,两个人,问的问题都比较简单,人也比较和蔼
c#有结构体么
c#的访问修饰符?都是什么意思?
介绍他们除了做语音识别,手写识别,还做无限运营,和三大运营商都有合作,特别是电信,排名前三,现在也开设做手机社区

人力面试
介绍了他们共is
技术二面
傲慢

CEO面试?
貌似进的的是总裁办公室,又问了我一遍工作经历,夫人干什么的,在哪儿住等等。。说这一行需要加班,后台出了什么问题,需要马上解决,小伙子你行么?我说IT就是这样的,我没问题

2010-07-18

静态构造,单例模式,和beforefieldinit


http://smartypeeps.blogspot.com/2007/03/beforefieldinit.html
"beforeFieldInit" is a special flag marked by compiler to the types which doesn't have static constructor.
This special flag tells "calling a static method does not force the system to initialize the type." Means that calling a static method does not make sure the static variables are initialized in the type.
public class ResourceClass
{
      private static StreamWriter sw = new StreamWriter();
      public static StreamWriter GetInstance()
      {
          return sw;
      }
}
In the above type it is not guranteed that when you call GetInstance the static variable "sw" would be created. Because of the reason the class is marked as "beforeFieldInit".
public class ResourceClass
{
    private static StreamWriter sw = new StreamWriter();
    static ResourceClass() { };
    public static StreamWriter GetInstance()
    {
         return sw;
    }
}
In the above type it is guranteed that when you call GetInstance the static variable "sw" would be created. Because of the reason the class contains "static constructor".
Properties of static constructor
Static constructors are not inherited, and cannot be called directly.
The exact timing of static constructor execution is implementation-dependent, but is subject to the following rules:
The static constructor for a class executes before any instance of the class is created.
The static constructor for a class executes before any of the static members for the class are referenced.
The static constructor for a class executes after the static field initializers (if any) for the class. The static constructor for a class executes, at most, one time during a single program instantiation.
The order of execution between two static constructors of two different classes is not specified.
But CLI does insist that all of the field variables will be initialized as soon as one of the static fields is accessed. So if we are depending on some side effects based on static variable initialization, then we may be waiting for a long time that to happen.
---------------------------------------------------------------
http://www.yoda.arachsys.com/csharp/singleton.html
Fourth version - not quite as lazy, but thread-safe without using locks
public sealed class Singleton
{
    static readonly Singleton instance=new Singleton();


    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()
    { }
    Singleton() { }
    public static Singleton Instance
    {
       get
       {
           return instance;
       }
    }
}

As you can see, this is really is extremely simple - but why is it thread-safe and how lazy is it? Well, static constructors in C# are specified to execute only when an instance of the class is created or a static member is referenced, and to execute only once per AppDomain. Given that this check for the type being newly constructed needs to be executed whatever else happens, it will be faster than adding extra checking as in the previous examples. There are a couple of wrinkles, however:
  • It's not as lazy as the other implementations. In particular, if you have static members other thanInstance, the first reference to those members will involve creating the instance. This is corrected in the next implementation. 
  • There are complications if one static constructor invokes another which invokes the first again. Look in the .NET specifications (currently section 9.5.3 of partition II) for more details about the exact nature of type initializers - they're unlikely to bite you, but it's worth being aware of the consequences of static constructors which refer to each other in a cycle. 
  • The laziness of type initializers is only guaranteed by .NET when the type isn't marked with a special flag called beforefieldinit. Unfortunately, the C# compiler (as provided in the .NET 1.1 runtime, at least) marks all types which don't have a static constructor (i.e. a block which looks like a constructor but is marked static) as beforefieldinit. I now have a discussion page with more details about this issue. Also note that it affects performance, as discussed near the bottom of this article. 
One shortcut you can take with this implementation (and only this one) is to just make instance a public static readonly variable, and get rid of the property entirely. This makes the basic skeleton code absolutely tiny! Many people, however, prefer to have a property in case further action is needed in future, and JIT inlining is likely to make the performance identical. (Note that the static constructor itself is still required if you require laziness.)

Fifth version - fully lazy instantiation
public sealed class Singleton
{
Singleton()
    {
    }
    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }


    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }
        internal static readonly Singleton instance = new Singleton();
    }
}

Here, instantiation is triggered by the first reference to the static member of the nested class, which only occurs in Instance. This means the implementation is fully lazy, but has all the performance benefits of the previous ones. Note that although nested classes have access to the enclosing class's private members, the reverse is not true, hence the need for instance to be internal here. That doesn't raise any other problems, though, as the class itself is private. The code is a bit more complicated in order to make the instantiation lazy, however.

2010-07-17

用友面试

时间:2010-7-16 星期五
地点:用友软件园
用友软件园的环境没的说,优雅安静,除了门口有门卫,研发中心都是自动门,不需要刷卡进入。

需要的人员是性能调优方面的,比如sql优化,调试工具的使用,自己在这方面的经验不足。。。
sql 表 物理连接的三种方式

风软面试

时间:2010-7-17 星期六
地点:中关村南大街韦伯时代中心C座702
这是一家做期货,风控的金融公司,规模不大,进门居然需要换鞋
首先做题:
一块豆腐切三刀,最多能切成几块?
一个电饼铛,每次能烙两张饼(单面),现有三张饼,每面烙一分钟,要求三分钟烙完,如何烙?
过河问题:现有男主人,女主人,两个仆人,一条狗要过河,船只能容纳两个人(狗),而且女主人和仆人在一起的话,女主人会杀死仆人,仆人和狗在一起的话,狗会咬死仆人,仆人在一起的话,仆人会逃跑,如何过河?
用类图描述文件夹,文件,快捷方式的关系
简述.net的异常处理机制,并举例
datareader 和dataset 的区别
编程实现冒泡排序
编程实现输出N以下的质数
多线程编程中lock,monitor,mutex 的区别
int a =5;
int b=5;
if(object.ReferenceEquals(a, b) == true)
   Console.WriteLine("equal");
esle
   Console.WriteLine("not equal");

当然是输出not equal,引文值类型会被装箱

int i = 5; int j = 5;
if ( Object.
ReferenceEquals( i, j ))
Console.WriteLine( "Never happens." );
else
Console.WriteLine( "Always happens." );
if ( Object.
ReferenceEquals( i, i ))
Console.WriteLine( "Never happens." );
else
Console.WriteLine( "Always happens." );
再看引用类型
string literal1 = "a"; 
string literal2 = "a"; 
Console.WriteLine(object.ReferenceEquals(literal1, literal2));
literal1 = literal2; 
Console.WriteLine(object.ReferenceEquals(literal1, literal2));
两者都输出 true,这是因为.net的string interning机制,literal1 和 literal2指向相同的内存地址
参见
How to: Compare Strings (C# Programming Guide)
面试过程:首先可能是个项目经历面试的,让我介绍了一下项目经历,
用过那些框架,
最近去没去面试,结果如何,
去了上周去xxx面的
家住哪里,
XXX,
那边也有很多it公司,没去试试么?
去面了几个
结果怎么样,
我居然说没结果,
他问为什么?我很诚实的说可能自己开发经验不多吧。。。
问我数学怎么样,
还可以吧
平时读什么技术书,
我说编程珠玑,代码大全,c# 4 in a nutshell, 
都读完了么?
代码大全读了一遍
书里的内容都应用到工作中了么?
代码大全里的东西用上 了,代码重构什么的
。。。
你是不是特别?
特别什么?
就是别人问一句答一句
嗯,和别人交流方便不太主动
然后说想找一个人写一个自动化测试的东西,问我想不想做,我说可以,他让我等一下,一会回来让我去另一个老总的办公室。老总说我们想做一个自动化测试平台,你能做了么?我说能吧,然后又问了写测试方面的东西,说你做测试用的是别人的东西,核心东西没学到,开发经验也少,问我要多少,我说7000,他说专门测试的话有点多,低了考虑么?我说不考虑,他说那只能我们考虑了。。。。面试结束

万网面试

时间:2010-7-15 星期五 雨
地点:鼓楼外大街北站,万网大厦

(需带简历)周五下午去万网面试,先去的后楼,一楼进门就是前台,后面墙上是一个倒着的“网”字,可能类似于福倒(到)了的意思吧。照例还是填写应聘信息表,做题。由于来面试的人很多,期间被赶来赶去好多次,从进门的茶几到小会议室,从小会议室再回来。体量很大,居然让我30分钟做完。做完后前台把我填的应聘信息,简历,我做的题顶起来,让我去前楼六楼找杨得祥面试
题目
一、简述概念:
XML,云计算,SMTP,DNS,TCP/IP,虚拟主机,IIS,Http,

二、有一个文件,里面有一百万个域名,输出重复的

三、有两个表:Department(DeptID,DepartName),Employee(EmpID,DeptID,EmpName,Salary)
编写Sql语句,找出平均工资大于2000的部门及其平均工资

四、overriding 和 overload 的区别

五、datareader 和 dataset 的区别

六、如何阻止某一个ip访问你的网站

七、以下程序的输出:
    try{Console.WriteLine("aaa");return true;}
    catch{Console.WriteLine("bbb");return true;}
    finally{Console.WriteLine("ccc");return true;}
八、四个填空题

  1. 。。。
  2. 。。。
  3. 。。。
  4. 万网的网址是,此题我一开始答的是www.net.com,在去了前楼六楼找面试官的时候,在前台改成了www.net.cn
面试
招聘的岗位是自动化的创建站点,帮助客户排除故障,问的问题很多都和网络相关:子网掩码;阻止某个ip段;c#操作注册表;电脑不能上网,如何排除故障;如何用c#代码创建站点。。。。回答的不好,面试官说如有复试,五日内给大幅

2010-07-08

SQL Server 2005's new added Ranking functions

Ranking functions return a ranking value for each row in a partition. Depending on the function that is used, some rows might receive the same value as other rows. Ranking functions are nondeterministic.

Transact-SQL provides the following ranking functions:

The following shows the four ranking functions used in the same query. For function specific examples, see each ranking function.

USE AdventureWorks2008R2;
GO
SELECT p.FirstName, p.LastName
,ROW_NUMBER() OVER (ORDER BY a.PostalCode) AS 'Row Number'
,RANK() OVER (ORDER BY a.PostalCode) AS 'Rank'
,DENSE_RANK() OVER (ORDER BY a.PostalCode) AS 'Dense Rank'
,NTILE(4) OVER (ORDER BY a.PostalCode) AS 'Quartile'
,s.SalesYTD, a.PostalCode
FROM Sales.SalesPerson s
INNER JOIN Person.Person p
ON s.BusinessEntityID = p.BusinessEntityID
INNER JOIN Person.Address a
ON a.AddressID = p.BusinessEntityID
WHERE TerritoryID IS NOT NULL
AND SalesYTD <> 0;





Here is the result set.





FirstName



LastName



Row Number



Rank



Dense Rank



Quartile



SalesYTD



PostalCode



Michael



Blythe



1



1



1



1



4557045.0459



98027



Linda



Mitchell



2



1



1



1



5200475.2313



98027



Jillian



Carson



3



1



1



1



3857163.6332



98027



Garrett



Vargas



4



1



1



1



1764938.9859



98027



Tsvi



Reiter



5



1



1



2



2811012.7151



98027



Shu



Ito



6



6



2



2



3018725.4858



98055



José



Saraiva



7



6



2



2



3189356.2465



98055



David



Campbell



8



6



2



3



3587378.4257



98055



Tete



Mensa-Annan



9



6



2



3



1931620.1835



98055



Lynn



Tsoflias



10



6



2



3



1758385.926



98055



Rachel



Valdez



11



6



2



4



2241204.0424



98055



Jae



Pak



12



6



2



4



5015682.3752



98055



Ranjit



Varkey Chudukatil



13



6



2



4



3827950.238



98055