Solved

Sending WinPopup messages

Posted on 2002-04-08
1
431 Views
Last Modified: 2009-07-29
I wont to send winpopup messages from my Java application. I'm working on Win98 so i can't use net send as I could using WindowsNT!
0
Comment
Question by:ljuso
1 Comment
 
LVL 3

Accepted Solution

by:
msterjev earned 150 total points
ID: 6927461
JNI solution:

The Java class:

public class WinPopup
{
     static
     {
          System.loadLibrary("WinPopup");
     }
     
     protected String from;
     
     public WinPopup(String from)
     {
          this.from=from;
     }
     
     public boolean sendMessage(String computer,String to,String message)
     {
          return sendMessage(computer,from,to,message);
     }
     public native boolean sendMessage(String computer,String from,String to,String message);
}

Generate the header file:

javah WinPopup

Generate new dll project and add WinPopup.h into the project.
Add new C++ file and paste this:

#include <windows.h>
#include "WinPopup.h"

JNIEXPORT jboolean JNICALL Java_WinPopup_sendMessage(JNIEnv * env, jobject obj, jstring c, jstring f,jstring t, jstring m)
{
     
     BOOL success=false;
     DWORD msgLength;
     DWORD bytesWritten;

     const char * computer=env->GetStringUTFChars(c,0);
     const char * from=env->GetStringUTFChars(f,0);
     const char * to=env->GetStringUTFChars(t,0);
     const char * message=env->GetStringUTFChars(m,0);
     
     //Prepearing WinPopup message

     msgLength=lstrlen(from)+lstrlen(to)+lstrlen(message)+4;
     char * winPopMsg=new char[msgLength];
     lstrcpy(winPopMsg,from);
     lstrcpy(winPopMsg+lstrlen(from)+1,to);
     lstrcpy(winPopMsg+lstrlen(from)+1+lstrlen(to)+1,message);

     //Prepearing Mailslot

     
     char mailslot[256];
     lstrcpy(mailslot,"\\\\");
     lstrcat(mailslot,computer);
     lstrcat(mailslot,"\\mailslot\\messngr");

     //Opening Mailslot
     
     HANDLE fileHandle=CreateFile(mailslot,GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
     if(fileHandle!=NULL)
          success=WriteFile(fileHandle,winPopMsg,msgLength,&bytesWritten,NULL);

     //Cleaning

     delete winPopMsg;
     env->ReleaseStringUTFChars(c,computer);
     env->ReleaseStringUTFChars(f,from);
     env->ReleaseStringUTFChars(t,to);
     env->ReleaseStringUTFChars(m,message);

     if(success)
          return JNI_TRUE;
     else
          return JNI_FALSE;
}

Put the generate dll into the same directory as WinPopup class.
Test it with:

import java.io.*;

public class Test
{
     public static void main(String[] args)throws Exception
     {
          WinPopup wp=new WinPopup("Myself");
          String comp="Comp1";
          String to="Someone";
          String line;
          DataInputStream in=new DataInputStream(System.in);
          while(!(line=in.readLine()).equals(""))
          {
               wp.sendMessage(comp,to,line);
          }
     }
}


0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question