preventing second executable starting

G

Guest

Hello. I have written a simple image viewer application using C# .NET. It
only needs to display one image at a time. When a different program, in C,
running on the same machine, does a "system(myfile.a2d2)" call, it
automatically starts my image viewer application (using file type
association). a2d2 is the image file format, my own. If the C program does
the system call again for a different image, I find that it starts a new
instance of my image viewer application running. I would like to have it so
that the already-running instance senses that another image is to be loaded,
and loads it, rather than starting a whole second instance of the
application. Any help on this OS and application question would be
appreciated.
 
D

DanS

Hello. I have written a simple image viewer application using C# .NET.
It only needs to display one image at a time. When a different
program, in C, running on the same machine, does a
"system(myfile.a2d2)" call, it automatically starts my image viewer
application (using file type association). a2d2 is the image file
format, my own. If the C program does the system call again for a
different image, I find that it starts a new instance of my image
viewer application running. I would like to have it so that the
already-running instance senses that another image is to be loaded,
and loads it, rather than starting a whole second instance of the
application. Any help on this OS and application question would be
appreciated.

the totally wrong group for this, but, what i think you would need to do is
add code to your program at startup to look to see if a previous instance
of it is running.

if it does see a previous instance of itself, the new instance needs to
communicate back to the first that it should open a new image, send the new
file name as a parameter back to it somehow, maybe DDE (old), thru a custom
message maybe, and then close itself, and then the first instance will open
the new file.

regards,

DanS
 
G

Guest

Hi Dan. Thank you for your reply and the outline of the strategy to do this.
This is uncharted waters for me, so if anyone has a more complete explanation
of the way it should be done, please let me know.
John
 
D

DanS

Hi Dan. Thank you for your reply and the outline of the strategy to do
this. This is uncharted waters for me, so if anyone has a more
complete explanation of the way it should be done, please let me know.
John

John,

There are LOADS and LOADS of web pages dedicated to programming. Do a
google search. Off hand, you can search planet-source-code.com.

There's 2 function's here, 1) detecting an existing instance, and 2)
inter-process communications. Those are the 2 items to search for. On
that specific page, there's a search right at the very top, and to the
right of the search string field there a drop-down box with which
language you want to look for.

Good Luck,

DanS
 
G

Guest

Thank you again Dan. I have done a lot of searching, and didn't find any way
to communite between a vanilla C program and a C# .NET executable. I thought
pipes was the answer, but I don't think C# .NET supports those. I found a
different way around the whole problem, though. I DID however, learn a little
about remoting, MSMQ, processes, etc., though, so it wasn't a waste of time
really.
John
 
G

Guest

Hello Dan. I posted the same question on dotnet.general after you advised me
of targeting the wrong group. This is a very useful response I got; thought
you might be interested too.


Hi John, have a look at this . . . It's not C# but not that difficult to
convert . .(if you want the original text look for PrevInstance in MSDN)

PrevInstance Property Changes in Visual Basic .NET
See Also
App Object Changes in Visual Basic .NET

In Visual Basic 6.0, the PrevInstance property of the App object was used to
determine if a previous instance of an application was running. There is no
equivalent for this property in Visual Basic .NET; however, the following
code can be used to test for a previous instance:

' Visual Basic .NET
Function PrevInstance() As Boolean
If
Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName))
Return True
Else
Return False
End If
End FunctionNote The behavior is slightly different than that of Visual
Basic 6.0. In Visual Basic 6.0, PrevInstance returned true only if the full
path and file name were exactly the same; in Visual Basic .NET, this
function will return true for two instances started from different paths. In
addition, PrevInstance would never return true for the first instance of an
application; in Visual Basic .NET, once a second instance is loaded, the
first instance will also return true.
JohnFol


John Mann
 
D

DanS

Hello Dan. I posted the same question on dotnet.general after you
advised me of targeting the wrong group. This is a very useful
response I got; thought you might be interested too.


Hi John, have a look at this . . . It's not C# but not that difficult
to convert . .(if you want the original text look for PrevInstance in
MSDN)

PrevInstance Property Changes in Visual Basic .NET
See Also
App Object Changes in Visual Basic .NET

In Visual Basic 6.0, the PrevInstance property of the App object was
used to determine if a previous instance of an application was
running. There is no equivalent for this property in Visual Basic
.NET; however, the following code can be used to test for a previous
instance:

' Visual Basic .NET
Function PrevInstance() As Boolean
If
Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCu
rrentProcess.ProcessName))
Return True
Else
Return False
End If
End FunctionNote The behavior is slightly different than that of
Visual Basic 6.0. In Visual Basic 6.0, PrevInstance returned true only
if the full path and file name were exactly the same; in Visual Basic
.NET, this function will return true for two instances started from
different paths. In addition, PrevInstance would never return true for
the first instance of an application; in Visual Basic .NET, once a
second instance is loaded, the first instance will also return true.
JohnFol
<SNIP>

I've found that even though sometimes you run across a lot of information
that may not be relevent to the matter at hand, this infomation can be
valuable at a later date. So it usually isn't a waste of time.

I as of yet have not made the switch to .Net, although every one screams
at me when I say I haven't. VB6 and VB.NET are really totally different
languages, which means I would be starting from scratch again, almost.

I don't really think that the .NET function above is really valid as a
'previous instance' call. Think about it, if instance 1 is started from
c:\program.exe and instance 2 is started from c:\test\program.exe, it is
not really a previous instance of the same program. In fact, I could
write a internet app called Net.exe and write a hockey game named Net.exe
and I think the function above would consider this the same program.

Please correct me if I am mistaken.

DanS
 
G

Guest

Hello Dan. I haven't used the PrevInstance member, but it seems that you're
correct; that it can't tell the difference between two programs which have
the same name.
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top