如何让IntPtr指向一个字符串

yooono 2007-06-22 04:03:56
IntPtr idp= IntPtr.Zero;
StringBuilder idata = new StringBuilder("000000");
string idata ="000000";
我这样建立的2个idata字符串,如何让idp指向他
我指向他的目的是为了传递给dll的某个函数,他需要传指针
还有我定义了一个结构如何向dll的函数传递其指针?
http://community.csdn.net/Expert/topic/5612/5612998.xml?temp=.9482843

http://community.csdn.net/Expert/topic/5614/5614773.xml?temp=.5012018
所有分了,解决一起结了。
...全文
564 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
soupsea 2008-07-29
  • 打赏
  • 举报
回复
String s = "Hello";
IntPtr p = Marshal.StringToHGlobalAnsi(s);
yooono 2007-06-29
  • 打赏
  • 举报
回复
没分了
yooono 2007-06-29
  • 打赏
  • 举报
回复
555555
wzd24 2007-06-25
  • 打赏
  • 举报
回复
结贴!!结贴!!
bote_china 2007-06-23
  • 打赏
  • 举报
回复
举个例子
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, ref int wParam, ref int lParam);
如果Api方法需要返回这个参数,那么就用ref string paramname来定义参数。

yooono 2007-06-23
  • 打赏
  • 举报
回复
这个字符串dll会读取修改并返回给c#的,用string 会被写保护的
bote_china 2007-06-23
  • 打赏
  • 举报
回复
.net里API调用没有必要考虑字符串指针吧?应该定义为stirng就可以,结构也是如此,只要自定义结构中的元素和API的保持一致就行。你可以参考一下.net框架的基类是如何调用API的,比如常见的事件消息MSG结构是如何定义的,GDI绘图中用到的rect结构是如何定义和使用的。界面API的封装在System.Windows.Forms.UnsafeNaiveMethods类中,需要用反编工具才能查看。
yooono 2007-06-23
  • 打赏
  • 举报
回复
public struct CINFTBL
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
public String htid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public String htdmy;
public long adv;
public long fileno;
public long filesize;
public long jobcmd;
public long datatype;
}

这结构
函数里面需要调用c原形这样

Function :
After polling, acquire the table data stored the communication management information. When the first data after polling is acquired, call this command. Call hst_getcinfnext by the next programming.
Form:
int hst_getcinffirst(CINFTBL *cinftbl)
Parameter:
typedef struct CINFTBL { Data acquirement territory for the stored table of the
communication management information.
UCHAR htid[6] ID letter line
Set ID letter line of dt700/750 in sending management.
UCHAR htdmy[2] Dummy
This command is no meaning.
long adv Priority Order
Priority order is set.
long fileno File number
Set the sending file number in requesting the sending file from dt700/750.
long filesize Total of file size
Set the total sending file in requesting the sending file from dt700/750.
long jobcmd Management request command
Set the management command requested from dt700/750 in establishing the
data linking.
0:file receiving(PC←HT)、1:file sending(PC→HT)、
255:cutting request
long datatype Kind of data
Set the kind of the objected data requested the management fromdt700/750.
} cinftbl
yooono 2007-06-23
  • 打赏
  • 举报
回复
qq:465731031我的
yooono 2007-06-23
  • 打赏
  • 举报
回复
可以,怎么联系你呀
wzd24 2007-06-23
  • 打赏
  • 举报
回复
能不能把你的DLL拿过来让我看看??
yooono 2007-06-23
  • 打赏
  • 举报
回复
to zswang(伴水清清)(专家门诊清洁工) :
我想用IntPtr取出当前字符串的指针然后传递到我将要用到的dll函数中,然后这个dll函数会根据这指针对这个StringBuilder 进行读取写入操作,然后返回其值c#环境中进行操作
==============================================
指向StringBuilder 暂时先不管他,因为我用IntPtr idp= IntPtr.Zero;
这idp是0得值也可以调用该函数,而且这函数返回值我先略过去
现在我想让IntPtr idp= IntPtr.Zero作为一个结构的指针传递到dll中去又出现问题了。
问题估计是一种情况,就是我在c#里声明的无论字符串也好结构也好,哪怕传递了ref 进去照样是"写保护"的,dll是无法进行写操作的,我该怎么往里传递呢?
wzd24 2007-06-23
  • 打赏
  • 举报
回复
用GCHandle.Alloc(object obj)方法来给string分配一个指针!
killer_liqiao 2007-06-23
  • 打赏
  • 举报
回复
怎么一点都不懂~~~
UP
王集鹄 2007-06-23
  • 打赏
  • 举报
回复
//参考标准API如何实现的

[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd,
StringBuilder lpString, int nMaxCount);

private void button1_Click(object sender, EventArgs e)
{
StringBuilder vStringBuilder = new StringBuilder(256);
GetWindowText(Handle, vStringBuilder, vStringBuilder.Capacity);
Console.WriteLine(vStringBuilder.ToString());
}
yooono 2007-06-23
  • 打赏
  • 举报
回复
郁闷好几天了,顶下
CSharpEx 2007-06-23
  • 打赏
  • 举报
回复
mark
蒋晟 2007-06-22
  • 打赏
  • 举报
回复
http://msdn2.microsoft.com/en-us/library/s97shtze(VS.80).aspx
兔子-顾问 2007-06-22
  • 打赏
  • 举报
回复
char[] charArray = StringBuilder.ToString().ToCharArray();
unsafe
{
fixed(char* p = &charArray[0])
{
//p就是字串首地址
}
}

110,536

社区成员

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

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

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