将VB代码转换为C#代码

fxy6781349 2012-06-13 11:24:19
以下是VB代码 如何转为C#代码 主要是调用api时Any不知道在C#如何表示等 求完整 麻烦了 谢谢
Public Const WM_COPYDATA As Integer = &H4A
Public Type COPYDATASTRUCT
dwData As Long
cbData As Long
lpData As Long
End Type

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal
cbCopy As Long)

Public Declare Function findWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Long

Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As
Long, ByVal wParam As Long, lParam As Any) As Long

'调用方法
Public Function CallCenterCall(tel As String)
On Error GoTo err:
Dim FT_hWnd As Long
Dim cds As COPYDATASTRUCT
Dim msg As String
'info = "companyid:'" & companyid & "',tel:'" & tel & "',uid:'" & uid & "',linkman:'" & linkman &

"',isout:'" & isout & "'"
msg = "tel:'" & tel & "'"
Dim buf() As Byte
ReDim Preserve buf(LenB(msg))

Call CopyMemory(buf(1), ByVal msg, LenB(msg))
cds.lpData = VarPtr(buf(1))
cds.cbData = LenB(msg)
cds.dwData = 0

'Dim pid

'pid = GetPsPid("CallCenterClient.exe")

FT_hWnd = findWindow(vbNullString, "呼叫中心")
'FT_hWnd = FindProcessWindow(pid)

If FT_hWnd <> 0 Then
SendMessage FT_hWnd, WM_COPYDATA, 0, cds
Else
MsgBox "呼叫中心未启动!", vbCritical, strSysName
End If
Exit Function

err:
MsgBox err.Description, vbCritical, strSysName
err.Clear
End Function

...全文
1554 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
dotnet90 2012-06-24
  • 打赏
  • 举报
回复
来晚了 呵呵
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
有熟悉VB的朋友帮忙看看啊 真的急用啊 非常感谢
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
这个是VB6.0写的 不是VB.NET哦 不会让我用VB.NET编译成DLL再看吧 不一定能成功吧
水牛刀刀 2012-06-13
  • 打赏
  • 举报
回复
第一个方法是上面有人说过的,用VB.NET TO C#的在线转换工具。既然你说不好用,那么可以这样:你把VB.NET代码编译了,然后用工具反编译(IL spy,reflector等)成C#代码。
sunylf 2012-06-13
  • 打赏
  • 举报
回复
public const int WM_COPYDATA = &H4A;
public struct COPYDATASTRUCT
long dwData;
long cbData;
long lpData;
}

public [DllImport("GAIS", SetLastError=true)] static extern void CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Any hpvDest, Any hpvSource, {
long cbCopy);

public [DllImport("GAIS", SetLastError=true)] static extern findWindow Lib "User32" Alias "FindWindowA" ( string lpClassName, {
string long lpWindowName);

private [DllImport("GAIS", SetLastError=true)] static extern SendMessage Lib "User32" Alias "SendMessageA" ( long hWnd, wMsg As {
long, long wParam, Any long lParam);

//调用方法
public CallCenterCall(string tel) {
On Error GoTo err:;
long FT_hWnd;
COPYDATASTRUCT cds;
string msg;
//info = "companyid://" & companyid & "//,tel://" & tel & "//,uid://" & uid & "//,linkman://" & linkman &

"//,isout://" & isout & "//";
msg = "tel://" & tel & "//";
buf(byte );
ReDim Preserve buf(LenB(msg));

Call CopyMemory(buf(1), msg, LenB(msg));
cds.lpData = VarPtr(buf(1));
cds.cbData = LenB(msg);
cds.dwData = 0;

// pid

//pid = GetPsPid("CallCenterClient.exe")

FT_hWnd = findWindow(vbNullString, "呼叫中心");
//FT_hWnd = FindProcessWindow(pid)

if ( FT_hWnd != 0 ) {
SendMessage FT_hWnd, WM_COPYDATA, 0, cds;
} else {
MsgBox "呼叫中心未启动!", vbCritical, strSysName;
}
Exit Function;

err:;
MsgBox err.Description, vbCritical, strSysName;
err.Clear;
}
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
我不纠结API 我纠结的是API的参数怎么调用 用工具转换为C#
[DllImport("kernel32.dll", EntryPoint="RtlMoveMemory")]
public static extern void CopyMemory(Any hpvDest, Any hpvSource, long cbCopy);

[DllImport("User32.dll", EntryPoint="FindWindowA")]
public static extern long findWindow(string lpClassName, string lpWindowName);

[DllImport("User32.dll", EntryPoint="SendMessageA")]
private static extern long SendMessage(long hWnd, long wMsg, long wParam, Any lParam);

如果将Any改为IntPtr C#API是没有什么问题的 那个调用呼叫中心的方法不知道该怎么改写 求大家帮忙 谢谢了
ParanoidKing 2012-06-13
  • 打赏
  • 举报
回复
那几个API到http://www.pinvoke.net/上去找现成的
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
C# code


[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static ex……
[/Quote]

这个我知道 嗯 不知道那个调用方法 怎么改写
zhujiawei7 2012-06-13
  • 打赏
  • 举报
回复
VB.Net to C# Converter
bdmh 2012-06-13
  • 打赏
  • 举报
回复


[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern int FindWindow(string cls, string wndwText);
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
csdn呆过几年了 在CSDN自己发过2次求助帖子 第一次发帖不知道是分数低了还是怎么了 无满意答案结贴 这次希望不一样的结局啊 csdn别让我失望啊
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
在这个网址去转换,不完全准确
http://www.developerfusion.com/tools/convert/vb-to-csharp/
[/Quote]

这个我转换过的 很多转换不出来 比如Any 在C#里面不知道转换成什么 VarPtr这些也没有办法转换 求帮忙 VB不熟悉
EnForGrass 2012-06-13
  • 打赏
  • 举报
回复
在这个网址去转换,不完全准确
http://www.developerfusion.com/tools/convert/vb-to-csharp/
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
急用 大家帮帮忙啊
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
唉 终于解决了 将整个数组地址找到存入 感谢各位朋友的帮助 结贴
/// <summary>
/// 调用呼叫中心
/// </summary>
/// <param name="tel"></param>
public static void CallCenterCall(string tel)
{
try
{
IntPtr FT_hWnd;
COPYDATASTRUCT cds;
string msg = "tel:'" + tel + "'";
byte[] buf = Encoding.Default.GetBytes(msg);


GCHandle gh = GCHandle.Alloc(buf, GCHandleType.Pinned);
IntPtr ptr = gh.AddrOfPinnedObject();
gh.Free();

cds.lpData = (int)ptr;
cds.cbData = msg.Length;
cds.dwData = 0;


FT_hWnd = WinAPI.FindWindow(null, "呼叫中心");

if (FT_hWnd != IntPtr.Zero)
{
IntPtr lParam = Marshal.AllocHGlobal(Marshal.SizeOf(cds));
Marshal.StructureToPtr(cds, lParam, true);
WinAPI.SendMessage(FT_hWnd, WinAPI.WM_COPYDATA, IntPtr.Zero, lParam);
Marshal.FreeHGlobal(lParam);
}
else
{
MessageBox.Show("呼叫中心未启动!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
qxyywy 2012-06-13
  • 打赏
  • 举报
回复
小子 跑这儿来要代码来了 公司资源不用
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
不会就这样沉下去了吧 顶起啊 未解决啊
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
楼上的朋友 我想问问
Call CopyMemory(buf(1), ByVal msg, LenB(msg))
cds.lpData = VarPtr(buf(1))
cds.cbData = LenB(msg)
cds.dwData = 0
这个地方怎么改呢
fxy6781349 2012-06-13
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]
试试这样

1、FindWindow和SendMessage指定用ASCI会不会有错?
2、为什么是cds.lpData用buf(1)的指针而不是整个buf的?
[/Quote]

那个是以前VB的代码 我不是很清楚为什么只用buf(1) 我现在改C# 不一定要写成buf(1) 只要能实现相同功能就行 试试上面朋友们写的 看看行不行
ciwodio 2012-06-13
  • 打赏
  • 举报
回复
public const int WM_COPYDATA = 0x4A;

public enum COPYDATASTRUCT
{
public int dwData;
public int cbData;
public int lpData;
}

[DllImport("kernel32.dll")]
extern static void RtlMoveMemory(object hpvDest,object hpvSource,int cbCopy);

[DllImport("User32.dll")]
extern static IntPtr FindWindow(string lpClassName,string lpWindowName);

[DllImport("User32.dll")]
extern static int SendMessage(IntPtr hWnd,int wMsg,int wParam,object lParam);

public void CallCenterCall(string tel)
{
try
{
IntPtr ftHwnd = IntPtr.Zero;
COPYDATASTRUCT cds = new COPYDATASTRUCT();
string msg = string.Empty;


msg = string.Format("tel:'{0}'",tel);

byte[] buffer = Encoding.Default.GetBytes(msg);

ftHwnd = FindWindow(null,"呼叫中心");

if(ftHwnd.Equal(IntPtr.Zero))
{
SendMessage(ftHwnd,WM_COPYDATA,0,buffer);
}
else
{
MessageBox.Show("呼叫中心未启动",strSysName,MessageBoxButtons.OK,MessageBoxIcons.Error);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,strSysName,MessageBoxButtons.OK,MessageBoxIcons.Error);
}
}
加载更多回复(2)

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧