Developer Support Languages VC++, C# and VB.NET
Welcome to Microsoft Developer Support, Languages team blog! You will find a lot of language related troubleshooting resources here.
Troubleshooting PInvoke Related Issues
I am back with some more PInvoke Stuff. Recently I was working on a PInvoke issue which I found interesting.
I have a C++ dll which has a function whose signature is
int TestFunc(IN_STRUCT in_Params, RET_STRUCT * pret_Par).
I wanted to call this function from C#. Function has two arguments. First argument is input structure which will be filled from C# code and passed to C++ code. Second argument is output structure which is filled in C++ code and output to C# code.
Here are the C struct definitions and a function that needs to be marshaled
#include "stdafx.h"
#include <stdio.h>
#include "Objbase.h"
#include <malloc.h>
typedef struct IN_STRUCT
{
BYTE CMD_PType;
BYTE CMD_PTType;
BYTE CMD_OC;
BYTE CMD_Seq;
};
typedef struct RET_STRUCT
{
BYTE RET_OC;
BYTE RET_Seq;
BYTE RET_RetBytes;
char *str;
BYTE RET_PD[10];
};
extern "C" __declspec(dllexport) \
int TestFunc(IN_STRUCT in_Params, RET_STRUCT * pret_Par)
{
int iRet = 0;
pret_Par->RET_OC = in_Params.CMD_OC;
pret_Par-
Ïà¹ØÎĵµ£º
·½·¨Ò»£º±éÀúÕýÔÚÓÐÏàͬÃû×ÖÔËÐеÄÀý³Ì
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace DHPSS.COM
{
/// <summary>
/// c#³ÌÐòÖ»ÔËÐÐÒ»´Î£¬C#Àý³ÌÖ»ÔËÐÐÒ»´Î
/// http://blog.csdn.net/nnsw ......
c#±à³Ì»ù´¡Ö®¶þ£ºÌØÐÔ ÕâÀïµÄÌØÐÔÇø±ðÓÚÊôÐÔ£¬ËäÈ»msdnÖÐËÆºõ²¢Ã»ÓÐÇø±ð¶þÕß¡£
ÊôÐÔ£ºÊÇÃæÏò¶ÔÏó±à³ÌµÄ»ù±¾¸ÅÄÌṩÁ˶Ô˽ÓÐ×ֶεķÃÎÊ·â×°£¬ÔÚC#ÖÐÒÔgetºÍset·ÃÎÊÆ÷·½·¨ÊµÏֶԿɶÁ¿ÉдÊôÐԵIJÙ×÷£¬ÌṩÁ˰²È«ºÍÁé»îµÄÊý¾Ý·ÃÎÊ·â×°¡£
ÌØÐÔ£º¹«¹²ÓïÑÔÔËÐÐʱÔÊÐíÌí¼ÓÀàËÆ¹Ø¼ü×ÖµÄÃèÊöÉùÃ÷£¬½Ð×öattributes, Ëü¶Ô³ÌÐòÖеÄÔ ......
1. ¼òÊö private¡¢ protected¡¢ public¡¢ internal ÐÞÊηûµÄ·ÃÎÊȨÏÞ¡£
´ð . private : ˽ÓгÉÔ±, ÔÚÀàµÄÄÚ²¿²Å¿ÉÒÔ·ÃÎÊ¡£
protected : ±£»¤³ÉÔ±£¬¸ÃÀàÄÚ²¿ºÍ¼Ì³ÐÀàÖпÉÒÔ·ÃÎÊ¡£
public : ¹«¹²³ÉÔ±£¬ÍêÈ«¹«¿ª£¬Ã»ÓзÃÎÊÏÞÖÆ¡£
internal: ÔÚͬһÃüÃû¿Õ¼äÄÚ¿ÉÒÔ·ÃÎÊ¡£
2 .ÁоÙASP.N ......
ÄãÊ×ÏÈÒªÀí½âһϸÅÄ
Ò» ÀàÐÍ(Type) ¶ÔÏóÊÇʲô
±ÈÈç object x; xÊǶÔÏó£¬object¾ÍÊÇËüµÄÀàÐÍ£¬ÔÚ³ÌÐòÖÐÈçºÎÃèÊöÀàÐÍÕâ¸ö¸ÅÄîÄØ£¿
¾ÍÊÇType(System.Type)¡£Òª»ñȡij¸öÀàµÄÀàÐÍ¿ÉÒÔÓÃtypeof()²Ù×÷·û
object a;object b; DataTable t;
aType = typeof(object);Type bType = typeof(object);tType = typ ......
1. newÓм¸ÖÖÓ÷¨
3ÖÖ ÊµÀý¶ÔÏó ·½·¨Òþ²Ø
2.ÈçºÎ°ÑÒ»¸öarray¸´ÖƵ½arrayListÀï
foreach( object o in array )arrayList.Add(o);
3.datagrid.datasouse¿ÉÒÔÁ¬½ÓʲôÊý¾ÝÔ´
Êý¾Ý¼¯DataSet
4.¸ÅÊö·´ÉäºÍÐòÁл¯
¶¯Ì¬»ñµÃ ......