473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to create a function called from an exe?

I'm an experienced foxpro, vba, delphi, sql programmer but this is something
new to me. I'm creating a pretty simple vb.net exe (scantextfiles.exe) that
will run on a server and read some text files into a database. But I also
need to create a stand alone function that can be called by
scantextfiles.exe. This stand alone function will use the MeasureString
method to get the length of some of the text in the text files. I'm not
putting that function in scantextfiles.exe because I need to call it from a
couple other places as well. So I'm thinking this needs to be a dll that I
can just plop into a folder somewhere. But I have really no idea how to do
that. I've never created a dll before. I'm not sure that's the best way to
go anyway. To make things a little more complicated (maybe) I'm using vb
2005 express to do all of this.

I created a test dll project and my class1.vb code is as follows (just to
try all this out):

Imports System.Math

Public Class Class1

Private Function fncSqRt(ByVal inputval As Double) As Double

If inputval < 0 Then
Return -1
Else
Return Sqrt(inputval)
End If

End Function

End Class

I compiled the dll and so I have TestDLL.dll sitting in my release folder.

Ok, so now what? :-) I really am not sure if I'm headed in the right
direction. I'm also not sure how to call the dll from scantextfiles.exe. I
know a lot of this is going to sound pretty basic but like I said, this is a
new area to me. Any help or pionting me in the right direction would be
appreciated.

TIA,

Keith

Aug 13 '08 #1
4 1584
Keith G Hicks wrote:
I'm creating a pretty simple vb.net exe (scantextfiles.exe) that
will run on a server and read some text files into a database.
You might have to consider writing it as a Windows Service unless you
can guarantee that you can run your program on the server's console.
Luckily, that's not such a Big Deal as it used to be.
I also need to create a stand alone function that can be called by
scantextfiles.exe. This stand alone function will use the MeasureString
method to get the length of some of the text in the text files. I'm not
putting that function in scantextfiles.exe because I need to call it from a
couple other places as well. So I'm thinking this needs to be a dll that I
can just plop into a folder somewhere.
That's the easiest way to reuse it, as it's loaded and executed at
run-time. However, you can still "share" code between projects by
"linking" the source files from one project into one or more /other/
projects (although, to be honest, building the shared stuff into a dll
/is/ probably easier to manage, in the long run).
But I have really no idea how to do that.
I've never created a dll before.
Reading ahead, I think you /have/. :-)
I created a test dll project and my class1.vb code is as follows (just to
try all this out):
Public Class Class1
/Public/ Function fncSqRt(ByVal inputval As Double) As Double
. . .
End Function
End Class

Your .exe project will only be able to see things that are Public in
your dll project (to start with, anyway).
I compiled the dll and so I have TestDLL.dll sitting in my release folder.
See? You /have/ built a dll.
Ok, so now what? :-) I really am not sure if I'm headed in the right
direction.
So far, so good.
I'm also not sure how to call the dll from scantextfiles.exe.
From your .exe project, add a Reference to the dll you've compiled
(you'll have to Browse to it). Don't be surprised when a copy of this
dll "magically" appears alongside your .exe - that's how Visual Studio
handles this.

Next, in your .exe project code, create an instance of your class

Private Sub Button1_Click( ... ) _
Handles Button1.Click

Dim c1 as New Class1
Dim d2 as Double = c1.fncSqRt( 99 )

DirectCast( sender, Button ).Text _
= d2.ToString( "0.000" )

End Sub

There's loads more that /can/ be done as you go forward with this
("fixing" the Assembly Version, putting the "shared" assembly into the
Global Assembly Cache, and so on), but that should get you started.

HTH,
Phill W.
Aug 13 '08 #2
On Aug 13, 6:39 pm, "Keith G Hicks" <k...@comcast.netwrote:
I'm an experienced foxpro, vba, delphi, sql programmer but this is something
new to me. I'm creating a pretty simple vb.net exe (scantextfiles.exe) that
will run on a server and read some text files into a database. But I also
need to create a stand alone function that can be called by
scantextfiles.exe. This stand alone function will use the MeasureString
method to get the length of some of the text in the text files. I'm not
putting that function in scantextfiles.exe because I need to call it froma
couple other places as well. So I'm thinking this needs to be a dll that I
can just plop into a folder somewhere. But I have really no idea how to do
that. I've never created a dll before. I'm not sure that's the best way to
go anyway. To make things a little more complicated (maybe) I'm using vb
2005 express to do all of this.

I created a test dll project and my class1.vb code is as follows (just to
try all this out):

Imports System.Math

Public Class Class1

Private Function fncSqRt(ByVal inputval As Double) As Double

If inputval < 0 Then
Return -1
Else
Return Sqrt(inputval)
End If

End Function

End Class

I compiled the dll and so I have TestDLL.dll sitting in my release folder..

Ok, so now what? :-) I really am not sure if I'm headed in the right
direction. I'm also not sure how to call the dll from scantextfiles.exe. I
know a lot of this is going to sound pretty basic but like I said, this is a
new area to me. Any help or pionting me in the right direction would be
appreciated.

TIA,

Keith
Keith,
After you compiled your DLL (class), now you're ready to reference it
from your main .exe project by following:

1- Open your main project,
2-In Solution Explorer -Right click your project
3-Click "Add Reference"
4-Go to "Browse" tab and locate your DLL if your DLL isn't present at
the same project (you could have included it in the same project and
could have called it directly, doesn't matter)

..and now you can instantiate your class from your main project and
use the functions like from a button click event or elsewhere:

'-------Begin--------

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim instance As New Class1
instance.fncSqRt("parameter_here")

End Sub

'-------End-----
Hope this helps,

Onur Güzel

Aug 13 '08 #3
OK. It's working. Thank you for all the notes on this (Onur too). But I have
a couple of questions.

First, some samples I found earlier had this sort of thing in the calling
exe's code:

Imports FNTNoticeImport.GetRoot

Module GetRoot

Declare Auto Function GetMyRoot Lib "C:\My
Projects\ImportEmailedNoticesIntoFNT\TestDLL\bin\R elease\TestDLL.dll" (ByVal
x As Double) As Double

End Module

Does adding the "reference" to the project mean that's not necessary? I did
it as you suggested below and commented out the above and it all worked. I'm
guessing the idea above might be for older types of code (vb6 manybe?)? That
didn't work anyway because I kept getting entry point errors.

Second, since I had to browse to the reference, and it seems that the path
to the dll is stored in the project, how do I deal with that? Does it just
"look like" a hard coded path? Since the compile process puts a copy of the
dll in the release folder, does it expect the dll to be in it's same
folder? What about calling the dll from another program as I mentioned I
need to do? Can I register the dll on a machine and call it from other
machines without them having to know exactly where it's located?
Keith

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-kwrote in message
news:g7**********@south.jnrs.ja.net...
Keith G Hicks wrote:
I'm creating a pretty simple vb.net exe (scantextfiles.exe) that
will run on a server and read some text files into a database.

You might have to consider writing it as a Windows Service unless you
can guarantee that you can run your program on the server's console.
Luckily, that's not such a Big Deal as it used to be.
I also need to create a stand alone function that can be called by
scantextfiles.exe. This stand alone function will use the MeasureString
method to get the length of some of the text in the text files. I'm not
putting that function in scantextfiles.exe because I need to call it
from a
couple other places as well. So I'm thinking this needs to be a dll that
I
can just plop into a folder somewhere.

That's the easiest way to reuse it, as it's loaded and executed at
run-time. However, you can still "share" code between projects by
"linking" the source files from one project into one or more /other/
projects (although, to be honest, building the shared stuff into a dll
/is/ probably easier to manage, in the long run).
But I have really no idea how to do that.
I've never created a dll before.

Reading ahead, I think you /have/. :-)
I created a test dll project and my class1.vb code is as follows (just
to
try all this out):

Public Class Class1
/Public/ Function fncSqRt(ByVal inputval As Double) As Double
. . .
End Function
End Class

Your .exe project will only be able to see things that are Public in
your dll project (to start with, anyway).
I compiled the dll and so I have TestDLL.dll sitting in my release
folder.
>
See? You /have/ built a dll.
Ok, so now what? :-) I really am not sure if I'm headed in the right
direction.

So far, so good.
I'm also not sure how to call the dll from scantextfiles.exe.

From your .exe project, add a Reference to the dll you've compiled
(you'll have to Browse to it). Don't be surprised when a copy of this
dll "magically" appears alongside your .exe - that's how Visual Studio
handles this.

Next, in your .exe project code, create an instance of your class

Private Sub Button1_Click( ... ) _
Handles Button1.Click

Dim c1 as New Class1
Dim d2 as Double = c1.fncSqRt( 99 )

DirectCast( sender, Button ).Text _
= d2.ToString( "0.000" )

End Sub

There's loads more that /can/ be done as you go forward with this
("fixing" the Assembly Version, putting the "shared" assembly into the
Global Assembly Cache, and so on), but that should get you started.

HTH,
Phill W.


Aug 13 '08 #4
Keith G Hicks wrote:
OK. It's working. Thank you for all the notes on this (Onur too). But I have
a couple of questions.

First, some samples I found earlier had this sort of thing in the calling
exe's code:

Imports FNTNoticeImport.GetRoot
Module GetRoot
Declare Auto Function GetMyRoot Lib "C:\My
Projects\ImportEmailedNoticesIntoFNT\TestDLL\bin\R elease\TestDLL.dll" (ByVal
x As Double) As Double
End Module
Does adding the "reference" to the project mean that's not necessary?
Yes.

Adding references and creating objects is the ".Net way" of using
..Net-created dll's. The "Declare" method, above, (and it's newer
equivalent, the "DllImport" Attribute) are used to access native Windows
dll's. Unless you need to reach into, say, the Windows API, I'd
recommend you avoid them and stick to references.
I did it as you suggested below and commented out the above and it
all worked. I'm guessing the idea above might be for older types
of code (vb6 maybe?)?
Actually, more like C++ and even Assembler!
That didn't work anyway because I kept getting entry point errors.
It's quite difficult to build a native dll in .Net. Not impossible, but
more often than not just not worth the effort.
Second, since I had to browse to the reference, and it seems that the path
to the dll is stored in the project, how do I deal with that? Does it just
"look like" a hard coded path? Since the compile process puts a copy of the
dll in the release folder, does it expect the dll to be in it's same
folder?
At run-time, it will load this dll from the directory that contains the
program that needs it so deploy both .exe and .dll alongside one another
and things should work.
What about calling the dll from another program as I mentioned I
need to do? Can I register the dll on a machine and call it from other
machines without them having to know exactly where it's located?
Ah; now that's one of the "next steps" - adding your Assembly to the
Global Assembly Cache. For that, you need to make some changes to your
dll project:

(1) "Fix" the Assembly version.
In the AssemblyInfo file (or wherever your version of 'Studio sets it),
set the Assembly Version to a fixed value, say "1.0.0.0". Doesn't
matter /what/ value, just get rid of those asterisks ("1.0.*")! The
asterisks cause the Assembly Version to be "generated" each time you
open the project and, when the GAC is involved, a different assembly
/version/ effectively means a totally different /assembly/.

(2) Strongly Name the assembly.
For this, you need a Strong Naming Key (.snk) File. Create yourself one
and put it in a /very/ safe place. IMHO, you should use the /same/ .snk
file for every Assembly that you want to Strongly Name. (I'm afraid
it's that long since I created mine, I've forgotten how!).
Include this in your project settings and rebuild the assembly.

(3) Use GacUtil to "register" it.
Find gacutil.exe (part of the SDK) and feed your dll into it:

....\gacutil.exe -i <name>.dll

(Even better, set this up as an "External Tool" in studio - saves you
popping up DOS prompts all over the place).

With all that done, remove and re-add the reference in your .exe project
- you should notice that the local copy of the dll /disappears/. All
your programs should now go to the GAC to load your dll.
If you rebuild the dll assembly, remember to re-add it to the GAC - if
you don't, then adding a reference to it will go back to the original,
local copy way of working.

Of course, deploying the dll assembly to (and registering in on) your
client machines might prove a little interesting ... ;-)

HTH,
Phill W.
Aug 14 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
2
by: Bill Nguyen | last post by:
How can I create a shared function to display process progress that can be called from other routines within an application? Any example that I can follow? Thanks Bill
4
by: Joe HM | last post by:
Hello - I have a Base Class where I want a New() implemented that can be called from the outside. This New() should create an instance of the appropriate cDerivedX Class ... The following...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.