|
i have solved the problem: these are the steps i have followed
1.- Create a java class with native methods. Create code to load the dll library.
Example:
import java.io.File;
/**
* @author hectorhg
*
*/
public class CLangWrapper {
/**
* Path of the .dll file
*/
public static final String dllName = "C:\\clangwrapper.dll";
// Code to Load the dll
static{
try{
File l_file = new File(".");
System.load(dllName);
}
catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
catch (Exception ex){
ex.printStackTrace();
}
}
/*********************************************
* N A T I V E M E T H O D S *
*********************************************/
/**
* Starts wrapper
* @return
*/
public static native int wrapperStart();
}
2.- javac myclass.java
3.- javah -jni package.myclass # in the ../ directory of package.
4.- A .h file is generated. implement it in a .c file.
5.- I have used vs2008, to create a dll project. In this project i included those files (don't forget about jni.h and jni_md.h) and my .cpp files.
6.- in the .c file implementing the native methods.
struct CPLUPLUSCLASS cppClass; // Global variable
#if defined(__GNUC__) && defined(_WIN32)
extern "C" JNIEXPORT jint JNICALL Java_sidsaTvAppControl_CLangWrapper_wrapperStart
(JNIEnv *, jobject){
#else
JNIEXPORT jint JNICALL Java_sidsaTvAppControl_CLangWrapper_wrapperStart
(JNIEnv *, jobject){
#endif
jint l_retVal = 0;
l_retVal = cppClass.myFunc();
return l_retVal;
}
7.- Note that the file that implements the .h file generated by jni must be included in the dll.
8.- compile and place the .dll into the rigth directory according to your java class implementation.
9.- run your java application.
10.- Check everything is working and smile
I hope that this can help someone
Regards,
Hector.
|
|
|
|
|
Hi,
I am working on a C++/CLI project using winforms. I need to consume a 3rd party native C++ dll.
Can any one send me some sample code to do the same.
Thanks,
Pratap
|
|
|
|
|
Here's a sample (with bad variable names):
[System::Runtime::InteropServices::DllImport("kernel32", EntryPoint="AA", ExactSpelling=false, CharSet=System::Runtime::InteropServices::CharSet::Ansi, SetLastError=true)]
static int A(System::String ^x, int ^y);
David Anton
http://www.tangiblesoftwaresolutions.com
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
VB & C# to Java Converter
Java to VB & C# Converter
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: convert VB, C#, or Java to C++/CLI
|
|
|
|
|
|
vikramaditya234 wrote: I am trying to port the code to UNIX in C language
See the name of the forum? The questions here are about C++/CLI Managed and Mixed Mode development for the .NET platform. Is that what you are doing?
led mike
|
|
|
|
|
What does this have to do with Managed C++?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Hello all,(Sory about my English) Please help....
VC++ 2005.
I have a func that call to Socket,Bind, Listen & Receive.
I call this func from timer that see if the Client send data.
I get the data in the first call but in the second call I got errors.
I understand that I need to operate the Socket,Bind, Listen only once.
I got err when put this line in a global scope or as a member data in class declaration:
Socket^ listenerSocket = nullptr;
So, I tried to store it with the help of global pointer.
It is good till I call the funcs of the Socket type, after all it just pointer that set to some place in the memory.
Socket^ *GlistenerSocket = nullptr;
Socket^ *GserverSocket = nullptr;
int CMotoScopeDlg::DoSocketGet( String^ server )
{
Socket^ listenerSocket = nullptr;
listenerSocket = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp );
if (false == SocketBindFlag )
{
listenerSocket = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp );
GlistenerSocket = &listenerSocket ;
(*GlistenerSocket)->Bind(hostEndPoint ); //Only once
(*GlistenerSocket)->Listen( 4 );
SocketBindFlag = true;
}else{
listenerSocket = *GlistenerSocket; //check this. allocate place??
}
Socket^ serverSocket = (*GlistenerSocket)->Accept(); //ERROR at second time
...
I think that if I could put this Socket variable in the global place it will help.
Thanks in advance,
Eyal
|
|
|
|
|
cra034 wrote: I think that if I could put this Socket variable in the global place it will help.
Sure, maybe if you put everything in "global place" it will work. Where is that anyway, next to Park Place?
I think if you learn about Programming Logic and Design it will help.
Your not in VB Drag and Drop land anymore toto.
led mike
|
|
|
|
|
I'm working on a program that basically sits and acts as a system monitor. Two of the primary things that I want it to do are monitor system memory and display when the last time a virus scan was made. Any suggestions on how I might go about this?
Thanks in advance
|
|
|
|
|
|
|
|
Hi I cannot compile the following code
The error is
1>.\STL_CLR.cpp(3) : fatal error C1083: Cannot open include file: 'cliext/vector': No such file or directory
Here is my code
Person.h
#pragma once
using namespace System;
ref class Person
{
public:
Person() : firstName(" "), secondName(" ") {}
Person(String ^first, String^ second) : firstName(first), secondName(second) { }
~Person() { }
virtual String^ ToString() override
{
return firstName + L" " + secondName;
}
private:
String^ firstName;
String^ secondName;
};
STL_CLR.cpp
#include "Person.h"
#include "stdafx.h"
#include
using namespace System;
using namespace cliext;
int main(array<string> ^args)
{
vector<person^>^ people = gcnew vector<person^>();
String^ first;
String^ second;
Person^ person;
while (true)
{
Console::WriteLine(L"Enter a first name or press Enter to end: ");
first = Console::ReadLine();
if (first->Length == 0)
break;
Console::WriteLine(L"Enter a second name: ");
second = Console::ReadLine();
person = gcnew Person(first->Trim(), second->Trim());
people->push_back(person);
}
Console::WriteLine(L"\nThe persons in the vector are:");
for each(Person^ person in people)
Console::WriteLine("{0}", person);
return 0;
}
</string>
|
|
|
|
|
I've been tryin to debug this switch statement but i get error after error. The code is suppose to allow the user to enter a value between 1 and 3. and then some please help with this code:
#include <iostream>
using namespace std;
int main()
{
int num;
cout << " Enter an integer number: 0-3" << ;
cin >> num;
switch (num);
{
case 1:
if (num==0)
cout << ; "You Entered: " <<num << ;
break;
case 2:
cout << "You Entered: " << num <<;
break;
case 3:
cout << "You Entered: " << num = num * 2 <<;
break;
default;
cout << "Invalid Input" << endl;
}
return 0;
}
|
|
|
|
|
The code has lots of errors.
Ending lines with << won't work.
Extra semi-colons all over the place where they don't belong.
Clean those up and you have something like this:
int num;
cout << " Enter an integer number: 0-3";
cin >> num;
switch (num)
{
case 1:
if (num==0)
cout << "You Entered: " <<num;
break;
case 2:
cout << "You Entered: " << num;
break;
case 3:
cout << "You Entered: " << (num = num * 2);
break;
default:
cout << "Invalid Input" << endl;
}
That should compile.
As for the runtime logic - You check if num==0 under case 1...
num will ALWAYS be 1 if it gets there - why check if it's 0?
You ask the user to enter number 0-3, yet 0 is an invalid number.
If the user enters 3, you tell the user 6 was entered.
Also, this code has nothing to do with C++/CLI so you've posted on the wrong board.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
What is the equivalent of a window in .NET? Is it a form? I don't mean the visible UI window, but the window handle used as a message pump.
Consider the following situation - Application A loads DLL B (on its one and only thread). DLL B spawns a new thread to receive data, but has to send that data to application A on the same thread as it was loaded. The usual practice (at least I think so), is to have a hidden window within DLL B, created on the application's thread, and the data-receiving thread posts a windows message and the received data to that window handle. The window procedure then sends the data to application A.
If you were to implement this in C++/CLI, what would you use instead of the hidden window?
Here's the real problem I'm facing: we have such a system as above, originally developed as a MFC application and native C++ DLL. It works perfectly in this case. However, the application had to be converted to a windows service written in C#. In this case, PostMessage within the DLL stops working. Using it as a native C++ DLL or compiling with /clr makes no difference. The PostMessage function itself succeeds, but the message disappears into nowhere. Any idea what could be happening here? Would the solution be to replace the window with a form (using BeginInvoke)? So far, MSDN and Google have got me nowhere...
The real system is actually much more complicated than I've described, I've just tried to separate the problem area here.
Thanks in advance!
Last modified: 31hrs 59mins after originally posted --
|
|
|
|
|
A window should still work in the service - it just won't be visible
to any logged on user (not that it was anyway). You should be able to debug
and check if the window was created successfully (is its HWND valid?).
It's certainly not necessary to post messages to a window for interthread
communication, and in a performance situation could be less than ideal.
I would use callbacks and/or regular function calls and proper thread synchronization.
There's no reason for a UI thread unless you have a UI.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark, thanks for the reply!
Yes, I checked everything possible. As I said, PostMessage itself succeeds, its just the message that dissapears. In fact, the very same code works when called from a regular application.
You are right about not needing to post messages for interthread communication. It was just that all this was pre-existing code that we were hoping to reuse without a major rewrite, but it looks like I'll have to look into some other method after all...
|
|
|
|
|
We figured out the origin of the problem, here's the solution posted for anyone interested.
In hindsight, it was quite obvious really. The hidden window in question was created in a DLL loaded directly from the service's OnStart method. If it was loaded from a UI, the message loop would operate as expected and posted messaged be delivered, but in the service's thread, the message loop does not operate.
The brute-force solution is to spawn a thread from OnStart and load the DLL there. The thread then creates a window and waits in its message loop (until the service is terminated). Any windows created within the DLL then work as expected.
(looks like a subtle bug[^] to me... )
|
|
|
|
|
If you are running under Vista or Server2008, the service is in window station0, but the logged on user is in window station 1+. In Xp and otherearlier OS's this only happeded inf terminal Services was installe. You could count on the first logon using station 0, so windows messages could be used to communicate with the service. This will not work with Vista or Windows Servier 2008 and later OS.
See This blog article[^] and the white paper it links to for more detailed information.
Note that if your dll is running in the process of the service itself, window messaging will still work (Intra process communication), but if it is running in a client application in a user login (inter process communication), it will not, since the main session message pump is different - session 0 programs will never see session 1 windows messages and vice -versa.
|
|
|
|
|
I have database with many records I want to display each record by click its button, on the same textbox so how could I bind one textbox with many records and each record has own button.
|
|
|
|
|
Work with CurrencyManager[^] class. Set the Position property to change the current position.
|
|
|
|
|
Hi guys,
What is the most common methodology to do a self integrity check on your program and quit in case of any kind of modification (like cracking attempts)?
I did some research and CRC seems to be a good way but there are arguments about CRC vs MD5. Implementing md5 for self check is not possibe only if you use a preloader which extracts and checks the main program. You can't tell the md5 before you would compile your program.
I looking for a solution which can be implemented into my program not a 3rd party exe protector (which is the easy way to do this).
|
|
|
|
|
Since you're asking on a managed-code board...
There's some built-in stuff already available to you:
Assembly Security Considerations[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi all,
I have a problem with dialogs...
After using ShowDialog() method to show a dialog, memory usage (in Task manager) rises
Up about 3 Mega bytes. But after closing that dialog only 1 mega byte of memory releases.
I don't know how to free the extra memory that a dialog makes after closing it!
please help me...
Thank you.
Every new thing you learn,Gives you a new personality.
|
|
|
|