首页 > 编程知识 正文

js调用c怎么调(js调用c语言接口)

时间:2023-12-24 01:06:49 阅读:319709 作者:FILM

本文目录一览:

JavaScript-js和c语言可以相互调用吗

程序语言在底层实现方面很多都是共同的,C语言适用范围广,底层的一些东西基本上都用C,好学但不容易深入。

javascript看似简单,但不容易掌握。

如何在 iOS 平台上使用 Javascript 直接调用 Objective-C 方法

packagecom;importorg.geotools.data.shapefile.ShapefileDataStore;importorg.geotools.data.simple.SimpleFeatureCollection;importorg.geotools.data.simple.SimpleFeatureIterator;importorg.geotools.data.simple.SimpleFeatureSource;importorg.geotools.geojson.feature.FeatureJSON;importorg.geotools.geojson.geom.GeometryJSON;importorg.geotools.geometry.jts.JTSFactoryFinder;importorg.opengis.feature.simple.SimpleFeature;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importcom.vividsolutions.jts.geom.Geometry;importcom.vividsolutions.jts.geom.GeometryFactory;importcom.vividsolutions.jts.geom.MultiPolygon;importcom.vividsolutions.jts.io.ParseException;importcom.vividsolutions.jts.io.WKTReader;importwContour.Contour;importwContour.Global.Border;importwContour.Global.PointD;importwContour.Global.PolyLine;importwContour.Global.Polygon;importwContour.Interpolate;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.io.Reader;importjava.io.StringReader;importjava.io.StringWriter;importjava.io.Writer;importjava.net.MalformedURLException;importjava.nio.charset.Charset;importjava.util.*;

js中方法调用疑问

function A(){

B();

C();

}

function B(){

alert("B method");

//这里添加代码

c();//////////////调用C函数

}

function C(){

alert("C method");

}

我想写点东西让"C method"没法alert出来

js里怎样调用c#写的dll

js只支持Activex Dll(所谓的com组件).在C#里如下面的方法写.

然后js里注册该控件并调用.

里面有两个方法

Encrypt(string pToEncrypt,string sKey) //加密

Decrypt(string pToDecrypt,string sKey) //解密

然后你这样写代码:

using System;

using System.Runtime.InteropServices;

namespace 名称空间

{

// 首先建立接口,这个是Com必须使用的

[Guid("61BB24CB-4C2C-40f8-9E13-1AC5E558D56A")]

public interface IEncrypt

{

string Encrypt(string pToEncrypt,string sKey);

string Decrypt(string pToDecrypt,string sKey);

}

// 写接口的实现

[Guid("CB52E990-185E-4448-A7E8-C88ECAD563AB")]

public class 类名称 : IEncrypt

{

public string Encrypt(string pToEncrypt,string sKey)

{

// 复制FAQ加密代码

}

public string Decrypt(string pToDecrypt,string sKey)

{

// 复制FAQ解密代码

}

}

}

然后用vs.net打包成.dll类库文件,假设名称为MyEncrypt.dll

然后用如下工具

regasm MyEnCrypt.dll /tlb:MyEncrypt.tlb

这个.tlb文件就是类型库,可以由vb6和vc++6引用。

注意上面的guid是使用vs.net工具菜单里面的创建guid工具生成的。

还要注意,vs.net自动生成的assemblyinfo.cs文件中必须添加强名称和版本号,因为Com组件需要版本号,不要改动版本号,也不要用vs.net自动的1.*这样的版本号,最好使用

1.1.1.1这样的固定版本,而且必须一次添加,不能多次添加。

regasm后怎样在js里调用?

用你的例子是这样?

var ss = new ActiveXObject("MyEncrypt.类名称");

JS不可以调用普通的Dll动态链接库,但你可以使用new ActiveXObject调用ActiveX DLL,如:

script

set z_shell= CreateObject( "WScript.Shell" )

z_shell.Run( "command.com /c mkdir " + "c:NewDir")

/script

如何在IOS平台上使用js直接调用OC方法

在Cocos2d-JS v3.0 RC2中,与Android上js调用Java一样,Cocos2d-JS也提供了在iOS和Mac上js直接调用Objective-C的方法,示例代码如下:

1 var ojb = jsb.reflection.callStaticMethod(className, methodNmae, arg1, arg2, .....);

在 jsb.reflection.callStaticMethod 方法中,我们通过传入OC的类名,方法名,参数就可以直接调用OC的静态方法,并且可以获得OC方法的返回值。

•参数中的类名,只需要传入OC中的类名即可,与Java不同,类名并不需要路径。比如你在工程底下新建一个类 NativeOcClass ,只要你将他引入工程,那么他的类名就是 NativeOcClass ,你并不需要传入它的路径。

1 import Foundation/Foundation.h @interface NativeOcClass : NSObject +( BOOL )callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content; @end

方法

•js到OC的反射仅支持OC中类的静态方法。

•方法名比较要需要注意,我们需要传入完整的方法名,特别是当某个方法带有参数的时候,你需要将他的:也带上。根据上面的例子。此时的方法名字是callNativeUIWithTitle:andContent:,不要漏掉了他们之间的:。

•如果是没有参数的函数,那么他就不需要:,如下代码,他的方法名是 callNativeWithReturnString ,由于没有参数,他不需要:,跟OC的method写法一致。

1 +(NSString *)callNativeWithReturnString;

使用示例

•下面的示例代码将调用上面 NativeOcClass 的方法,在js层我们只需要这样调用:

1234 var ret = jsb.reflection.callStaticMethod( "NativeOcClass" , "callNativeUIWithTitle:andContent:" , "cocos2d-js" , "Yes! you call a Native UI from Reflection" );

•这里是这个方法在OC的实现,可以看到是弹出一个native的对话框。并把 title 和 content 设置成你传入的参数,并返回一个boolean类型的返回值。

123 +( BOOL )callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:@ "Cancel" otherButtonTitles:@ "OK" , nil]; [alertView show]; return true ; }

•此时,你就可以在 ret 中接受到从OC传回的返回值(true)了。

注意

在OC的实现中,如果方法的参数需要使用float、int、bool的,请使用如下类型进行转换:

•float,int 请使用NSNumber类型

•bool请使用BOOL类型。

•例如下面代码,我们传入2个浮点数,然后计算他们的合并返回,我们使用NSNumber而不是int、float去作为参数类型。

1 +( float ) addTwoNumber:(NSNumber *)num1 and:(NSNumber *)num2{ float result = [num1 floatValue]+[num2 floatValue]; return result;}

•目前参数和返回值支持 int, float, bool, string,其余的类型暂时不支持。

js如何调用c语言写的api

包含api的.h文件,然后直接调用就可以了,比如winsock.h,可以调用recv等函数,也可以在函数前面加::

答案补充

HWND hWnd = ::FindWindow(sClassName, sWindowName);//传入类明到sClassName,窗口名sWindowName,否则传入NULL

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。