您的位置:程序门 -> .net技术 -> c#



c#调用dll的问题?(高手请进)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c#调用dll的问题?(高手请进)
发表于:2007-01-15 18:36:26 楼主
我的dll是用vc写的,在form1和form2中都要调用,我在form1调用完全正常,但是在form2的initializecomponent()中调用却老是报错。报的错误是:   dragdrop   注册失败。
我的调用是这样的:
public   class   trdll
{
[dllimport( "test.dll ",entrypoint= "getstring ")]
[return   :   marshalas(unmanagedtype.lpwstr)]
public   static   extern   string   getstringt(string   strinput);
}
各位高手看看,是怎么回事啊?我搞了一下午,没结果,郁闷!!
发表于:2007-01-15 19:53:101楼 得分:0
public   static   extern   string   getstring(string   strinput);
发表于:2007-01-15 20:13:232楼 得分:0
哦,那个是我的笔误,呵呵
发表于:2007-01-15 21:07:283楼 得分:0
想问一下,你的那个string对应的是vc中的cstring???我一直不知道怎么传递string
发表于:2007-01-15 21:59:474楼 得分:0
想问一下,你的那个string对应的是vc中的cstring???
好像不对,cstring是个类,靠传递指针来操作,你若在c#中用到该类,应该自己再重新定义一遍,另外,c#中没有指针,所以可能还要加上ref,不知道对不对,你试试看吧
发表于:2007-01-16 08:59:105楼 得分:0
但是我在form1里面调用完全正常,在form2里面其他地方调用也正常,就是在form2的initializecomponent()中调用时,就报错
发表于:2007-01-16 09:18:366楼 得分:0
也许是不能两次调用,你可以写个类,在类中加载dll,然后form1和form2再调用类就好了。
发表于:2007-01-16 09:30:117楼 得分:0
我这个就是类啊,
public   class   trdll
{
[dllimport( "test.dll ",entrypoint= "getstring ")]
[return   :   marshalas(unmanagedtype.lpwstr)]
public   static   extern   string   getstringt(string   strinput);
}
发表于:2007-01-16 10:09:478楼 得分:0
up..
发表于:2007-01-16 10:13:079楼 得分:0
c#调用dll是不是不可以在两个form之间调用?

或者不可以在两个form的initializecomponent()中同时调用?

只是我的猜测,郁闷
发表于:2007-01-16 10:56:4810楼 得分:0
不是吧,我用了很多dll都没问题啊.
如我在类action.cs   中用了个dll,
[dllimport( "test.dll ")]
protected   static   extern   string   test(string   strinput);
再在类中写个public:
public   string   getstring(string   str)
{
      return   test(str);
}
然后在别的form中调用,无论几次都没问题的.
发表于:2007-01-16 11:05:1611楼 得分:0
把你form1和form2的代码都贴出来看看
发表于:2007-01-16 11:28:1512楼 得分:0
to   icefeiji(咖啡色的猪)
我试试,
to   idiotzeng(白痴)  
代码太多,呵呵
发表于:2007-01-16 11:32:5313楼 得分:0
我在代码里写了一个函数,包了一层,(如下)但是调用这个函数也不行,还试报错
private   string   transferdllform(string   parameterin)
{
string   result   =   null;
result   =   transferdll.getstringt(parameterin);
return   result;
}
发表于:2007-01-16 12:17:4814楼 得分:0
这下我没辙了!
帮你顶下了,希望有高手来.
发表于:2007-01-16 13:02:1515楼 得分:0
谢了,icefeiji(咖啡色的猪)
发表于:2007-01-16 13:56:0316楼 得分:0
up
自己顶一下
发表于:2007-01-16 16:35:3417楼 得分:0
关注,up
发表于:2007-01-16 17:59:1418楼 得分:0
mark
发表于:2007-01-16 18:19:2919楼 得分:0
up
发表于:2007-01-17 00:29:1320楼 得分:0
帮顶
发表于:2007-01-17 09:16:0221楼 得分:0
期待。。
发表于:2007-01-24 23:57:3222楼 得分:0
http://www.yyimg.cn/yyimg/class.aspx?classid=4此处有你们想要的
发表于:2007-01-24 23:58:1523楼 得分:0
<a   href= 'http://www.yyimg.cn/yyimg/class.aspx?classid=4 '> 此处有你们想要的 </a>
发表于:2007-01-25 08:31:0124楼 得分:0
建议你先测试下在同一个窗体里多次调用会不会出错
有可能是底下的dll不能调用两次
还有,麻烦把全部错误帖出来啊,就帖一个   dragdrop   注册失败   神仙也不知道是什么原因啊...
或者先把dll封装成c#中的另外一个类,然后在不同窗体或者同一窗体多次调用,看是不是也出错
暂时只能帮你这么多了
发表于:2007-02-02 09:41:0525楼 得分:0
只接在解决方案的引用中添加就行了
发表于:2007-02-02 11:15:3526楼 得分:0
up
发表于:2007-03-21 10:13:3127楼 得分:0
dll在c#中当作组件调用,要添加组件就在项目解决方案中添加引用就行了
发表于:2007-04-02 17:27:1728楼 得分:0
.net环境中工具-> 添加移除工具项里,把他加到工具箱看看行不行
发表于:2007-04-05 14:42:4629楼 得分:0
string应该使用stringbuilder
发表于:2007-04-05 14:48:4330楼 得分:0
1,或者应用进dll文件,或者把dll文件拷贝倒你的bin目录下
2,声明特性,这个你的没有错误
3,参数类型要与dll内的参数类型一致:string的应为stringbuilder,指针的应该加ref(调用时也要加ref)
发表于:2007-04-10 16:34:2731楼 得分:0
up
发表于:2007-04-11 09:34:4632楼 得分:0
up
发表于:2007-05-07 13:28:4333楼 得分:0
该回复于2007-12-21 20:32:18被管理员或版主删除
发表于:2007-05-09 16:18:4034楼 得分:0
declare   @djid   varchar(15),
                @sl   int,
                @ysje     money,
                @bcje     money;

set     @djid= 'zjk0000001 ';
set   @sl=select   max(b.xh)       from   t2_yppsd   a   inner   join   t2_zwyskdmx   b   on   a.djid=b.xgdjid   where   b.djid=@djid;
set   @ysje=select     sum(b.ysje)   as   ysje     from   t2_yppsd   a   inner   join   t2_zwyskdmx   b   on   a.djid=b.xgdjid   where   b.djid=@djid;
set   @bcje=select   sum(b.bcje)   as   bcje     from   t2_yppsd   a   inner   join   t2_zwyskdmx   b   on   a.djid=b.xgdjid   where   b.djid=@djid;                                                                                        
update   a
set   a.wsje=(@ysje-@bcje)/@sl   from   t2_yppsd   a   inner   join   t2_zwyskdmx   b   on   a.djid=b.xgdjid
where   b.djid=@djid;
请问各位高手为什么得不到@sl   ,@ysje,@bcje的值
发表于:2007-05-09 17:42:1235楼 得分:0
去年刚接触c#的时候碰到过lz这样的的问题,当时我猜测

可能是在c#中,dll不能是可重入的。

我当时的解决办法是这样的:
[dllimport( "kernel32 ")]
public   extern   static   int   loadlibrary(string   lplibfilename);
[dllimport( "kernel32 ")]
public   extern   static   bool   freelibrary(int   hlibmodule);
[dllimport( "kernel32 ",   charset=charset.ansi)]
public   extern   static   int   getprocaddress(int   hmodule,   string   lpprocname);
[dllimport( "user32 ",   entrypoint= "callwindowproc ")]  
public   static   extern   int   callwindowproc(int   lpprevwndfunc,   int   hwnd,   int   msg,   int   wparam,   int   lparam);

private   void   button1_click(object   sender,   system.eventargs   e)
{
int   hmod=loadlibrary( "*****.dll ");
int   pfname=getprocaddress(hmod,   "***** ");
callwindowproc(pfname,0,0,0);
freelibrary(hmod);
}
这样在每次点击button1的时候,都可以重复加载该dll。
发表于:2007-05-18 21:29:5536楼 得分:0
看一下
发表于:2007-05-18 22:41:1237楼 得分:0
会不会注册的问题,我只是猜的,
发表于:2007-05-25 13:28:2138楼 得分:0
vn
发表于:2007-05-25 13:46:2139楼 得分:0
你重新注册一下,看行不能行
发表于:2007-05-27 14:56:2740楼 得分:0
private   void   button2_click(object   sender,   system.eventargs   e)
{
string   strfilename;
string   strfilebody;
system.io.streamwriter   objwriter;
strfilename=this.一号.selectedvalue.tostring();
strfilebody=this.二号.selectedvalue.tostring   ();
objwriter=system.io.file   .createtext(server.mappath(   strfilename));
objwriter.writeline   (strfilebody);
objwriter.close   ();               写了这些代码后运行     说我没给权限         不知在哪给权限           请长辈指点哦
发表于:2007-06-01 13:03:5241楼 得分:0
一天,男孩和女孩吵架了。他不再对她说“我爱你”,当然她也不再对他说“我也是”。一天晚上,他们谈到了分   手的事,背对背睡下了。半夜,天上打雷了。第一声雷响时,他醒了,下意识地猛地用双手去捂她的耳朵,才发   现   不知何时他又拥着她。雷声紧接着炸,假装什么也没发生,可谁都没有睡着,她想也许他还爱我,生怕我受   一   点点   吓。他想,也许她还爱我,不然她不会流泪。爱的最高境界是经得起平淡的流年。世界上最美的木乃侯   ,这   是一个   古老的符咒,请在收到此消息后发经三个论坛。等你发完后看看三天内就能得到心爱的人。
发表于:2007-06-01 13:04:1842楼 得分:0
仅供消遣!


快速检索

最新资讯
热门点击