By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

how do I write code in javascript to use a function in C

P: 14
Suppose that I have some simple function in C like the following when I want to use it in javascript code

Expand|Select|Wrap|Line Numbers
  1. int plus1(int a)
  2. {
  3.     return a+1;
  4. }
I am using visual C++2008 for my C code.

I guess that I need to start by
file->new project->class library and call it with some name
let call it classtest

Now the visual C++ give me some files

I guess that I need to add some code to the file classtest.h and to file
classtest.cpp but I am not 100% sure what I need to add and I am not 100% sure what I need to add in javascript to use the function.

If you tell me what to do in this specific case then hopefully it is going to help me to use more complex functions that I have in C in javascript.

Note that I know how to do it only with javascript
and I simply do the following steps:

1)type the following code in a new asp file

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE="JavaScript" RUNAT=SERVER>
  2. function plus1(a)
  3. {
  4.   return a+1;
  5. }
  6. Response.Write(plus1(5));
  7. </SCRIPT>
2)save it in as javaexample.asp

3)goto
http://****/***/javaexample.asp

4)see 6 in the screen

I replaced some letters by **** but
**** is specific to the server that I am using that is only connected to the computer that I use even when I am not connected to the interenet so it is not important.
Nov 5 '09 #1
Share this Question
Share on Google+
10 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you canít* use C/C++ functions in JavaScript.

main reason: C++ => compiled in an application, JavaScript => parsed in the browserís sandbox.

to create JavaScript you only need a text editor (although tag highlighting and all the bells and whistles make it more comfortable) to write and a simple (X)HTML page to host/execute.

btw. what makes you think JavaScript is unable to do complex tasks, or is it just the urge to re-use already written code?

* - if you want to run the JS in the browser, other host applications may provide an API you can use.

PS.
Expand|Select|Wrap|Line Numbers
  1. a++;
Nov 5 '09 #2

P: 14
one of the reasons is that I want to re-use already written code but it is not the only reason.

C is also faster and people told me that I can use something like
server.createobject and dll but I am not sure exactly what I need to do for it.

Some step by step instructions can clearly help me.

If I can see step by step instructions for a simple example it can help me to use other functions that I already have code for them.

Uri
Nov 5 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
@urib
of course itís faster, compiled code always is (compared to parsed code).
Ö unless you have system interactions

@urib
never heard of that before. anyway, JavaScript does not have access to local files.

@urib
did I already mention that itís not possible?
Nov 5 '09 #4

P: 14
There is a difference between javascript in server and java script in client side.
I write javascript in runat server(see RUNAT=SERVER in my code).
Nov 5 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
I donít know ASP. and if you want to use the code on the server only, I donít see the need of using JavaScript at all, which is used (in almost every case) on the client side.

maybe youíre mistaking C# for JavaScript, they have a very similar syntax.
Nov 5 '09 #6

P: 14
Thanks for your replies.
Maybe I should go to asp.net forum.

The programmer that I talked with him agree that today javascript is not the best way to code on the server(I understood that he used javescript in the past but today he is using visual studio.net).

Uri
Nov 5 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
@urib
they should definitely know more about MS server scripting

@urib
Javascript has its strength in handling document content.

@urib
does that mean he uses JavaScript with that IDE?
Nov 5 '09 #8

Plater
Expert 5K+
P: 7,872
I believe you can use managed c++ as the backend for asP.NET pages in the same way you can use C# and VBNET. Not positive as I've never bothered with managed c++
Nov 5 '09 #9

P: 14
I asked the programmer that I talked with him again and he told me that
Today he is using vb.net that is only for server code when he use javascript for client code so I guess the right forum to ask about server coding is vb.net(I still did not decide where to ask my question)

Uri
Nov 5 '09 #10

Dormilich
Expert Mod 5K+
P: 8,639
I could help out with PHPÖ
Nov 5 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.